Restoring db connection

Hi, I am using the ORM library generated by VP.
An object is inserted into the db, let’s say once in few seconds by the Timer.
If some problem occurs with network connection to the database and an Exception if thrown, after the restoring of the network connection, anyway nothing can be saved, because getInstace() member is returning _instace with a connection which is no longer valid, and it does not go further until application (it is a service in my case) restart.
What I have done wrong, how can I bypass this? I could not find anything like this on the forum.
Thank you. ik

Hi ik,

We are now discussing your case. Will get back to you once we’ve came up and conclusion.

Best regards,
Jick

Thanks, may be additional information is needed?
The problem was discovered in logs, after a night without any activity, mysql probably closed the connection, and on next try to save, an exception is thrown. But probably I did something wrong… :roll:

Hi iK,

Sorry, but we were unable to reproduce the problem. In our testing, the connection pool will throw exception if connection lost, but it can recover itself when connection repaired.

In fact, the next call for getSession() will return a valid connection. I guess your main problem is that the connection idle has been too long. This can be fixed by setting timeout in connection pool, after the specified time, it’ll renew the connection.

Best regards,
Jick

[quote=ik]Hi, I am using the ORM library generated by VP.
An object is inserted into the db, let’s say once in few seconds by the Timer.
If some problem occurs with network connection to the database and an Exception if thrown, after the restoring of the network connection, anyway nothing can be saved, because getInstace() member is returning _instace with a connection which is no longer valid, and it does not go further until application (it is a service in my case) restart.
What I have done wrong, how can I bypass this? I could not find anything like this on the forum.
Thank you. ik[/quote]

Hi Ik,

I think I was experiencing this same problem you have. My code was aprox:

 
// C# code:
while (true) {
    PersistentSession s=Manager.Instance().GetSession();
    PersistentTransaction t=s.BeginTransaction();

    try {
        // Some method calls using persistence
        t.Commit();
    }
    catch {
        // Some error
        t.RollBack();
    }
}
 

Well… this went all ok except when an exception was thrown :evil: But… all I had to do to fix this was:

  • add “s.Close()” in the catch

So the final code was:

 
// C# code:
while (true) {
    PersistentSession s=Manager.Instance().GetSession();
    PersistentTransaction t=s.BeginTransaction();

    try {
        // Some method calls using persistence
        t.Commit();
    }
    catch {
        // Some error
        t.RollBack();
        s.Close();    // <------------------- NEW
    }
}
 

Then, this time the .GetSession() at the top of the while was returning a valid connection, session, etc.

BTW, this sample code was written without compiling it. Please excuse some syntax errors, you get the idea :roll:

Maybe this helps you.

Best regards