0

We are using a jTDS connector (v1.3.1) and Hibernate 3 to connect to a MSSQL Server 2008+ database (Cannot change versions on anything). Normal connectivity works fine and transactions process without issue. However, we are seeing endless amounts of transactions on our database in a sleeping/AWAITING COMMAND state. After using SQL Profiler we were able to trace it down to a few areas of our code where we keep the database connection open while doing bulk processing of data and only commit transactions while closing the PreparedStatements/Statements. Basically what we see on the database is this as our last transaction:

IF @@TRANCOUNT > 0 COMMIT TRAN 

This means that the connection is committing but never finalized so it stays in a sleep state. An example of our code:

Connection conn = ... //Open the connection object
conn.setAutoCommit(false);
while (true){
   PreparedStatement ps = null;
   try{
      ps = conn.prepareStatement("insert into dbtable (column1,column2) values ('data1','data2'); //Opens a new transaction
      ps.executeUpdate();
      conn.commit();
   }catch(SQLException ex){
      log.error(ex);
   }finally{
     DatabaseUtil.close(ps);
   }
}

The above is just an example, but essentially each prepared statement starts a new transaction and essentially stays open at each new statement. The transactions stay in a sleep state until the connection is closed.

Question: Should the transaction end at commit/close of the prepared statement? OR Is this normal behavior and we need to close our connection object every time?

0

1 Answer 1

0

There's nothing wrong with keeping the connection open if you plan to reuse it. Just make sure, as it seems like you're doing, to commit or rollback any transactions between calls

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.