Pergunta

I am a beginner in vb and mysql... I am working on a project (vb 2010 express and mysql5.1) where there are two machines in a lan network. The application is installed on both the machines and the database is on one of the machines. Both connect to the DB using same user,(dr_admin). My problem is that the changes made from one machine doesn't get reflected in the other machine.

DBCon = New ADODB.Connection
    DBCon.ConnectionString = ("Driver={MySQL ODBC 3.51 Driver};Server=Admin-PC;  Database=dr_db; User=dr_admin;Password=dradmin;pooling=false;Option=3;")
    DBCon.Open()
    MsgBox(DBCon.State)
    Try
        Cmd = New ADODB.Command
        Cmd.ActiveConnection = DBCon
        Cmd.CommandText = "SEt autocommit=0;"
        Cmd.Execute()

This is how i connect and i manually commit and rollback wherever needed.

I populate a list from the DB. The list is present on a panel. The data is loaded every time the panel becomes visible. But the new data doesn't show up. Then i added a bit of code to close current db connection and reopen again.

    Public Sub reconnect_pr()

    DBCon.Close()


    MsgBox(DBCon.State)
    DBCon = Nothing
    DBCon = New ADODB.Connection
    DBCon.ConnectionString = ("Driver={MySQL ODBC 3.51 Driver};Server=Admin-PC;Database=dr_db; User=dr_admin;Password=dradmin;pooling=false;Option=3;")
    DBCon.Open()
    MsgBox(DBCon.State)
    Try
        Cmd = New ADODB.Command
        Cmd.ActiveConnection = DBCon
        Cmd.CommandText = "SEt autocommit=0;"
        Cmd.Execute()
    Catch ex As Exception

        MsgBox("autocommit error - Contact admin", MsgBoxStyle.OkOnly, "Patient Care")

    End Try


End Sub

this is the code i use to retrieve the data from db.

      Cmd = New ADODB.Command
        Cmd.ActiveConnection = login_frm.DBCon
        Cmd.CommandText = "select pat_no,pat_id,pat_name from pat_token_details order by pat_no;"
        Rs = Cmd.Execute

        exp_tod_pat_list.Items.Clear()
        exp_tod_pat_list.Items.Add("None")
        Do While Not Rs.EOF
            v_token = Rs("pat_no").Value
            v_pat_name = Rs("pat_name").Value
            v_id = Rs("pat_id").Value

            exp_tod_pat_list.Items.Add("Token id: " + v_token + " - " + v_pat_name + " ( " + v_id + " )")
            Rs.MoveNext()
        Loop
    Catch ex As Exception
        MsgBox("unable to fetch current token details", MsgBoxStyle.OkOnly, "Token")
    End Try

Still it doesn't work.. But it gets reflected when i close the application and open it again.

Kindly help me understand the problem.

Thanks.

Foi útil?

Solução

A short explanation of a reason of this behaviour:

In MySql the default isolation level is REPEATABLE READ.
In this mode (see this link for details: click me):

All consistent reads within the same transaction read the snapshot established by the first read. This convention means that if you issue several plain (nonlocking) SELECT statements within the same transaction, these SELECT statements are consistent also with respect to each other.

In easy words: if you issue, within the same transaction, the same SELECT statement several times, you will always get the same results, regardless of changes of database made by other sessions.

The session remembers, that the first SELECT within the transaction was run at the time X and "makes the snapshot of data" at the time X, then all subsequent selects will read this snaphshot at time X, but not current data.


See next link: (click me):

If the transaction isolation level is REPEATABLE READ (the default level), all consistent reads within the same transaction read the snapshot established by the first such read in that transaction. You can get a fresher snapshot for your queries by committing the current transaction and after that issuing new queries.

By commiting the transaction (or issuing a rollback of the transaction), you end this transaction, and a next SELECT (within a new transaction) will see fresh data.

If auto_commit is enabled, then all selects are always automatically commited - so you always see fresh data.
But if you disable auto-commit, in repeatable-read (default) isolaton level, you must commit manually to see changes in the database.


Note: the above behaviour is true only for InnoDb tables. Other engines don't support ACID.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top