Reputation: 508
I am getting the error:
SQL State: 25001
Error Code: 20000
Message: Cannot close a connection while a transaction is still active.
I understand this because I am closing the db connection, when a transaction is not committed or rolled-back.
This should be fairly easy to fix. However, I am using a connection pool. So I am really not sure where this connection is being left uncommitted. I am just trying to gracefully close each connection in the connection pool.
Is it safe to just always call commit before closing the transaction?
if (!connection.isClosed())
{
connection.commit();
connection.close();
}
The other way to fix this is to carefully check each database action to assure the connection is committed. Is this a safe shortcut?
Upvotes: 2
Views: 344
Reputation: 6497
Log it and rollback.
Never blindly commit.
The connection pool should rollback any connection that is returned with an active transaction. The thinking here is that the reason the transaction is still active when the connection is being returned is that the borrowing thread had some sort of problem and didn't finish its process normally.
Upvotes: 1