FYI.
I came across an odd problem with the jTDS driver combined with the connection pooling ( DBCP or C3P0 ). jTDS uses a caching system which creates a temporary stored procedure for every unique query string and then re-executes the proc when the same query is rerun.
However this didn't seem to be working for me despite running exactly the same query. After some digging through the combined hibernate, connection pool and jTDS code I discovered that the following happened.
Code:
session.close()
calls:
connectionPool.releaseConnection()
calls:
jTDSConnection.rollback()
which deletes any stored procedures created in the current transaction.
And all because I was doing this everywhere:
Code:
Session session = factory.openSession()
session.find( query );
session.close();
and not using a transaction like this.
Code:
Session session = factory.openSession()
Transaction tx = session.beginTransaction();
session.find( query );
tx.commit();
session.close();
Because the session.find() will always open a transaction implicitly when the connection is released the pool will ensure it doesn't have a transaction still open by calling rollback() on it to cleanup. So using an explicit transaction and calling commit() will properly close the current transaction so the driver doesn't delete the stored procs.
Yet another reason to always use a transaction explicitly even when just using a find().
This was with SQLServer 2000, jTDS driver, DBCP or C3P0 pool and hibernate 2.1.1 but it may be relevant to other drivers/dbs/pools.