I read with interest the following post
http://forum.hibernate.org/viewtopic.php?t=928593, since I am getting the same type of error being : the JDBC connection does not close properly with my current settings for my web app.
Quote:
Quick Summary of problem: When using MS SQLServer 2000, I am getting java.lang.OutOfMemoryError exceptions in my weblogic 8.1 myserver_server.log. Additionally, in weblogic8.1's myserver_server_error.log I am getting "WARNING: afterTransactionCompletion() was never called
Mar 3, 2004 8:24:39 AM net.sf.hibernate.impl.SessionImpl finalize
WARNING: unclosed connection" repeatedly. Of important note here is that I never get this issue running the same codebase with MySQL v4. I only get these problems with SQLServer 2000.
My hibernate helper class does not work exactly the same way as in the linked post (the session was stored in a singleton). In my implementation, the session is passed in parameters and that is why I think implementing ThreadLocal pattern as suggested would not solve my problem.
Could you help me finding why these connection errors occur?
Additionnal Info:
I am working with IBM WebSphere Application Server, Release 4.0.6. I'm not sure of that, but I think it's using the connection pooling from my datasource.
Hibernate version: 2.1.6
Mapping documents: na
Code between sessionFactory.openSession() and session.close():
Code:
import net.sf.hibernate.Session;
import mypackage.Employee;
import mypackage.InfrastructureException;
public class EmployeeDAO {
.....
public Employee getEmployee(String emplId) {
Employee employee = null;
HibernateUtil.beginTransaction();
Session session = HibernateUtil.getSession();
try {
employee = (Employee) session.load(Employee.class, emplId);
HibernateUtil.commit(session);
} catch (HibernateException e) {
//TODO log in a file
e.printStackTrace();
HibernateUtil.rollback(session);
throw new InfrastructureException(e);
} finally {
HibernateUtil.closeSession(session);
}
return employee;
}
}
Here is my hibernate helper class
Code:
/*
* Created on 27-Sep-04
*
*/
package mypackage;
import java.sql.SQLException;
import mypackage.InfrastructureException;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory =
new Configuration()
.addResource("mypackage/MyMapping.hbm.xml")
.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
public static Session getSession() {
Session s;
try {
s = sessionFactory.openSession();
} catch (HibernateException e) {
throw new InfrastructureException(e);
}
return s;
}
public static void closeSession(Session s) {
try {
if (s != null && s.isOpen()) {
s.disconnect();
s.close();
}
} catch (HibernateException e) {
throw new InfrastructureException(e);
}
}
public static void beginTransaction() {
Transaction tx;
try {
tx = getSession().beginTransaction();
} catch (HibernateException e) {
throw new InfrastructureException(e);
}
}
public static void commit(Session s) {
try {
if (s != null) {
s.flush();
s.connection().commit();
}
} catch (HibernateException e) {
throw new InfrastructureException(e);
} catch (SQLException e) {
throw new InfrastructureException(e);
}
}
public static void rollback(Session s) {
try {
if (s != null) {
s.connection().rollback();
}
} catch (HibernateException e) {
throw new InfrastructureException(e);
} catch (SQLException e) {
throw new InfrastructureException(e);
}
}
}
Here is my hibernate.properties file
Code:
######################
### Query Language ###
######################
## define query language constants / function names
hibernate.query.substitutions true 1, false 0, yes 'Y', no 'N'
#################
### Platforms ###
#################
## JNDI Datasource
hibernate.connection.datasource jdbc/mydatabase
hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect
...
#######################
### Transaction API ###
#######################
## the Transaction API abstracts application code from the underlying JTA or JDBC transactions
#hibernate.transaction.factory_class net.sf.hibernate.transaction.JTATransactionFactory
hibernate.transaction.factory_class net.sf.hibernate.transaction.JDBCTransactionFactory
## to use JTATransactionFactory, Hibernate must be able to locate the UserTransaction in JNDI
## default is java:comp/UserTransaction
## you do NOT need this setting if you specify hibernate.transaction.manager_lookup_class
#jta.UserTransaction jta/usertransaction
#jta.UserTransaction javax.transaction.UserTransaction
#jta.UserTransaction UserTransaction
## to use JCS caching with JTA, Hibernate must be able to obtain the JTA TransactionManager
#hibernate.transaction.manager_lookup_class net.sf.hibernate.transaction.JBossTransactionManagerLookup
#hibernate.transaction.manager_lookup_class net.sf.hibernate.transaction.WeblogicTransactionManagerLookup
#hibernate.transaction.manager_lookup_class net.sf.hibernate.transaction.WebSphereTransactionManagerLookup
#hibernate.transaction.manager_lookup_class net.sf.hibernate.transaction.OrionTransactionManagerLookup
#hibernate.transaction.manager_lookup_class net.sf.hibernate.transaction.ResinTransactionManagerLookup
##############################
### Miscellaneous Settings ###
##############################
## print all generated SQL to the console
hibernate.show_sql true
## auto schema export
#hibernate.hbm2ddl.auto create-drop
#hibernate.hbm2ddl.auto create
#hibernate.hbm2ddl.auto update
## specify a JDBC isolation level
#hibernate.connection.isolation 4
## set the JDBC fetch size
#hibernate.jdbc.fetch_size 25
## set the maximum JDBC 2 batch size (a nonzero value enables batching)
hibernate.jdbc.batch_size 0
## enable use of JDBC 2 scrollable ResultSets (specifying a Dialect will cause Hibernate to use a sensible default)
#hibernate.jdbc.use_scrollable_resultset true
## use streams when writing binary types to / from JDBC
hibernate.jdbc.use_streams_for_binary true
## use JDBC 3 PreparedStatement.getGeneratedKeys to get the identifier of an inserted row
hibernate.jdbc.use_get_generated_keys false
## specify a default schema for unqualified tablenames
#hibernate.default_schema test
## use a custom stylesheet for XML generation (if not specified, hibernate-default.xslt will be used)
#hibernate.xml.output_stylesheet C:/Hibernate/net/sf/hibernate/hibernate-default.xslt
## enable outerjoin fetching (specifying a Dialect will cause Hibernate to use sensible default)
#hibernate.use_outer_join false
## set the maximum depth of the outer join fetch tree
hibernate.max_fetch_depth 1
## enable CGLIB reflection optimizer (enabled by default)
hibernate.cglib.use_reflection_optimizer false
##########################
### Second-level Cache ###
##########################
## optimize chache for minimal "puts" instead of minimal "gets" (good for clustered cache)
#hibernate.cache.use_minimal_puts true
## set a prefix for cache region names
hibernate.cache.region_prefix mypackage
## enable the query cache
hibernate.cache.use_query_cache true
## choose a cache implementation
hibernate.cache.provider_class net.sf.ehcache.hibernate.Provider
#hibernate.cache.provider_class net.sf.hibernate.cache.EmptyCacheProvider
#hibernate.cache.provider_class net.sf.hibernate.cache.HashtableCacheProvider
#hibernate.cache.provider_class net.sf.hibernate.cache.TreeCacheProvider
#hibernate.cache.provider_class net.sf.hibernate.cache.OSCacheProvider
#hibernate.cache.provider_class net.sf.hibernate.cache.JCSCacheProvider
#hibernate.cache.provider_class net.sf.hibernate.cache.SwarmCacheProvider
############
### JNDI ###
############
## specify a JNDI name for the SessionFactory
#hibernate.session_factory_name hibernate/session_factory
## Hibernate uses JNDI to bind a name to a SessionFactory and to look up the JTA UserTransaction;
## if hibernate.jndi.* are not specified, Hibernate will use the default InitialContext() which
## is the best approach in an application server
#file system
#hibernate.jndi.class com.sun.jndi.fscontext.RefFSContextFactory
#hibernate.jndi.url file:/
#WebSphere
#hibernate.jndi.class com.ibm.websphere.naming.WsnInitialContextFactory
#hibernate.jndi.url iiop://localhost:8080/
Full stack trace of any exception that occurs:
This Exception occurs after initialization of HibernateUtil SessionFactory
com.ibm.ejs.cm.exception.WorkRolledbackException: Outstanding work on this connection which was not comitted or rolledback by the user has been rolledback.
at java.lang.Throwable.<init>(Throwable.java)
at java.lang.Throwable.<init>(Throwable.java)
at java.sql.SQLException.<init>(SQLException.java:90)
at com.ibm.websphere.ce.cm.PortableSQLException.<init>(PortableSQLException.java:50)
at com.ibm.ejs.cm.exception.WorkRolledbackException.<init>(WorkRolledbackException.java:8)
at com.ibm.ejs.cm.pool.ConnectO.decRef(ConnectO.java:381)
at com.ibm.ejs.cm.pool.ConnectionPool.freeConnection(ConnectionPool.java:197)
at com.ibm.ejs.cm.proxy.ConnectionProxy.close(ConnectionProxy.java:861)
at net.sf.hibernate.connection.DatasourceConnectionProvider.closeConnection(DatasourceConnectionProvider.java:64)
at net.sf.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:91)
at net.sf.hibernate.cfg.Configuration.buildSettings(Configuration.java:1132)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:766)
at mypackage.HibernateUtil.<clinit>(HibernateUtil.java:52)
at java.lang.Class.initializeImpl(Native Method)
at java.lang.Class.initialize(Class.java)
at mypackage.EmployeeDAO.getEmployee(EmployeeDAO.java:28)
<= first call on HibernateUtil (initialization).
at mypackage.Logon.onSuccessfullLogon(Logon.java)
at mypackage.logonNT(Logon.java:327)
at mypackage.Logon.execute(Logon.java:88)
at mypackage.WebframeServlet.executeCommand(WebframeServlet.java:614)
at mypackage.WebframeServlet.performTask(WebframeServlet.java:1089)
at mypackage.WebframeServlet.doPost(WebframeServlet.java:457)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.ibm.servlet.engine.webapp.StrictServletInstance.doService(ServletManager.java:827)
at com.ibm.servlet.engine.webapp.StrictLifecycleServlet._service(StrictLifecycleServlet.java:167)
at com.ibm.servlet.engine.webapp.IdleServletState.service(StrictLifecycleServlet.java:297)
at com.ibm.servlet.engine.webapp.StrictLifecycleServlet.service(StrictLifecycleServlet.java:110)
at com.ibm.servlet.engine.webapp.ServletInstance.service(ServletManager.java:472)
at com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch(ServletManager.java:1012)
at com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch(ServletManager.java:913)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:721)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:374)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:118)
at com.ibm.servlet.engine.srt.WebAppInvoker.doForward(WebAppInvoker.java:134)
at com.ibm.servlet.engine.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:239)
at com.ibm.servlet.engine.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:67)
at com.ibm.servlet.engine.invocation.CacheableInvocationContext.invoke(CacheableInvocationContext.java:106)
at com.ibm.servlet.engine.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:154)
at com.ibm.servlet.engine.oselistener.OSEListenerDispatcher.service(OSEListener.java:317)
at com.ibm.servlet.engine.http11.HttpConnection.handleRequest(HttpConnection.java:60)
at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:391)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:284)
at com.ibm.ws.util.CachedThread.run(ThreadPool.java:144)
Debug level Hibernate log excerpt:
[08/11/04 16:54:48:439 EST] 19cc19cc SystemOut U 16:54:48,409=> WARN SettingsFactory:95 - Could not obtain connection metadata
[08/11/04 16:54:52:475 EST] 48ca48ca SystemOut U 16:54:52,475=> WARN SessionImpl:3396 - unclosed connection
[08/11/04 16:54:52:675 EST] 19cc19cc SystemOut U 16:54:52,665=> WARN JDBCExceptionReporter:20 - SQL Warning: 0, SQLState:
[08/11/04 16:54:52:695 EST] 19cc19cc SystemOut U 16:54:52,675=> WARN JDBCExceptionReporter:28 - [Microsoft][SQLServer 2000 Driver for JDBC]Database changed to mydatabase
[08/11/04 16:54:52:705 EST] 19cc19cc SystemOut U 16:54:52,695=> WARN JDBCExceptionReporter:20 - SQL Warning: 0, SQLState:
[08/11/04 16:54:52:725 EST] 19cc19cc SystemOut U 16:54:52,715=> WARN JDBCExceptionReporter:28 - [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Changed database context to 'feuille_temps'.
[08/11/04 16:54:52:745 EST] 19cc19cc SystemOut U 16:54:52,725=> WARN JDBCExceptionReporter:20 - SQL Warning: 0, SQLState:
[08/11/04 16:54:52:765 EST] 19cc19cc SystemOut U 16:54:52,745=> WARN JDBCExceptionReporter:28 - [Microsoft][SQLServer 2000 Driver for JDBC]Language changed to us_english
[08/11/04 16:54:52:795 EST] 19cc19cc SystemOut U 16:54:52,765=> WARN JDBCExceptionReporter:20 - SQL Warning: 0, SQLState:
[08/11/04 16:54:52:805 EST] 19cc19cc SystemOut U 16:54:52,795=> WARN JDBCExceptionReporter:28 - [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Changed language setting to us_english.
Name and version of the database you are using:Microsoft SQLServer 2000
The generated SQL (show_sql=true):na