Solved

manage remote MySQL transactions

Posted on 2011-09-22
7
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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