Question about DbPersistenceService
jesse_sweetland Mar 23, 2006 11:46 AMWe're trying to troubleshoot a connection leak that we think might be associated with our recent adoption of JTA and I ran across something interesting in DbPersistenceService.java. The getConnection(boolean resolveSession) method calls getDataSource(), which checks to see if there is a JNDI data source name registered on the DbPersistenceService. If so, then it gets a connection from that DataSource and returns it to open a Hibernate session with a user-supplied connection. My first question is: how does one register a JNDI data source name on the DbPersistenceService? We've specified a hibernate.connection.datasource property in Hibernate.cfg.xml, but I since the getDataSource() method is checked first I want to make sure that there is no other overriding configuration that might prevent Hibernate from using its data source.
My second question concerns the following block of code from the getSession() method:
Connection connection = getConnection(false); if (connection!=null) { log.debug("creating hibernate session with connection "+connection); session = getSessionFactory().openSession(connection); mustSessionBeClosed = true; mustSessionBeFlushed = true; mustConnectionBeClosed = false; } else { log.debug("creating hibernate session"); session = getSessionFactory().openSession(); mustSessionBeClosed = true; mustSessionBeFlushed = true; mustConnectionBeClosed = false; }
If a JNDI data source name was registered on the DbPersistenceService, and a connection was retrieved from that DataSource, then the first block is taken (user-supplied connection to the Hibernate session). I looked in the Hibernate ConnectionManager.java class and it looks like if a connection is supplied, then Hibernate does not close the connection. This being the case, it would seem that the mustConnectionBeClosed field should be set to "true" in top branch. The close() method of DbPersistenceService will not call connection.close() if it is false, and Hibernate does not close the connection since it was supplied to the session factory, so the connection is never closed and leaks. Is that accurate?
As an aside, has anyone had an issue using JTA in jBPM 3.1 on JBoss 4.0.3 with Oracle? The leak does not seem to happen in non-JTA environments, and it only happens from web services that are not backed by Session beans (web applications and session-bean-backed web services do not appear to leak).
Thanks,
- Jesse