Solved

manage remote MySQL transactions

Posted on 2011-09-22
7
365 Views
Last Modified: 2012-05-12
Experts -

New to MySQL backend.

I have small, very straightforeward VB.NET Windows Forms app that reads and writes to a remote MySQL instance. Only a few users, but a couple are in Asia, and the db resides in the Amazon Cloud.

Anyways, none of the writes are db intensive - in almost every case I'm hitting well organized indexes, mostly PKs. Still, I'm new to this type of architecture (and perhaps it's not perfect, it maybe screams out for a WEB-based app, but I'm not an ASP guy...). I'm mostly concerned about maintaining absolute transactional integrity. I would like some advice - code examples - regarding the best way to programmatically manage the sorts of inserts and updates and selects I'm performing. Any pointers to best practices in these various regards would be greatly appreciated.

Mostly, I'd like error handling code that helps to confirm connection status and manages rollbacks & commits.

*No points awarded if answers don't have code, links to code*

Insert\Update example:

Dim conn As New MySqlConnection(strMySQLConnectString)
                   
    Dim cmd As New MySqlCommand
    Dim params As MySqlParameterCollection = cmd.Parameters
    cmd.Connection = conn
    cmd.CommandType = CommandType.Text
    conn.Open()

    'updating user values with default values
    cmd.CommandText = "update users set name = 'Player' where id = " & strUserId & ""
    cmd.ExecuteNonQuery()
    'here we enter into the transaction_history table

    cmd.CommandText = "insert into transaction_history (user_id) values (" & CInt(Me.txtUserID.Text) & ");"
    cmd.ExecuteNonQuery()
    conn.Close()

Select example:

Dim strConnect As String = strMySQLConnectString
Dim FileSQL As String

FileSQL = "select users.* from users where id = " & CInt(Me.txtUserID.Text) & ";"

Dim conReader As New MySqlConnection(strConnect)
Dim conString As New MySqlCommand(FileSQL, conReader)
conReader.Open()

Dim drReader As MySqlDataReader
drReader = conString.ExecuteReader(CommandBehavior.CloseConnection)

Do While drReader.Read()
   
strUserId = drReader.Item("id").ToString
strUserName = drReader.Item("name").ToString
strUserEmail = drReader.Item("email").ToString
strUserCreateDate = drReader.Item("created").ToString
strUserExperience = drReader.Item("exp").ToString
strUserLevel = drReader.Item("level").ToString
strUserLosses = drReader.Item("losses").ToString
strUserRating = drReader.Item("rating").ToString
strUserRatingMu = drReader.Item("rating_mu").ToString
strUserRatingSigma = drReader.Item("rating_sigma").ToString
strUserGold = drReader.Item("gold").ToString
strUserCrystals = drReader.Item("crystals").ToString
strUserReferer = drReader.Item("referer").ToString
   
Loop

conReader.Close()
drReader.Close()

-----------------

TIA!

c.
0
Comment
Question by:crafuse
  • 4
  • 3
7 Comments
 

Author Comment

by:crafuse
ID: 36581991
bump

;-P
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36582171
You comment above had an opposite effect!!! EE sends out email notifications automatically if no comment is posted to a question after a given number of hours but you cancelled that.

Now to the point. Where are transactions? I dont see any. Is your concern referential integrity of data or concurrency violations?
0
 

Author Comment

by:crafuse
ID: 36582286

mostly concurrency issues, thought i was clear. you don't agree writes are transactions? anyways, i guess i'm mostly concerned that connections to the db are opened and closed successfully, and if there's any interruption within, that i can somehow flag this. i only write to a few key tables now and then, but it's important that data and transactional 'integrity' is maintained. i can't really afford any screw-ups...ideas?

(and no more bumps...)

c.

0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36582305
I thought you are talking about explicit transactions. As I said, if you have not gone much far in the development, consider doing this as a web. It will be very slow as a winforms app.
0
 

Author Comment

by:crafuse
ID: 36582360
it is already done, as a winforms app. should it be ported? would it be that much faster? why?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 36582393
>would it be that much faster? why?
Because it would not be transporting a lot of data over network.
0
 

Author Closing Comment

by:crafuse
ID: 36711785
feeble.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now