Hi
I've got troubles using Hibernate with Spring TransactionManager, it seems to come from hibernate configuration. The sessions are not closed, and I finally got JDBCException 'Too many connections' (with Mysql)
my hibernate.cfg.xml (for PostgreSQL, I changed to test, but the result is the same)
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- PostgreSQL -->
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">capomega</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.transaction.auto_close_session">true</property>
<property name="hibernate.current_session_context_class">jta</property>
<!-- Mapping files -->
<mapping resource="fr/vivreaction/hibernate/Action.hbm.xml" />
(...)
</session-factory>
</hibernate-configuration>
spring applicationContext.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="close">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!--
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="transactionProxy" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED, readOnly</prop>
</props>
</property>
<!--
-->
</bean>
<!--**********************************
BEANS Service
***********************************-->
<bean id="actionService" parent="transactionProxy">
<property name="target">
<bean class="fr.vivreaction.business.ActionService">
<property name="actionDao" ref="actionDao"/>
</bean>
</property>
</bean>
...
<!--**********************************
BEANS Dao
***********************************-->
<bean id="actionDao" class="fr.vivreaction.hibernate.dao.ActionDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
...
</beans>
One of my dao method.
Code:
Session session = getSession();
try {
Query query = session.createQuery("from Action");
List<Action> result = new ArrayList<Action>();
result.addAll(query.list());
return result;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
log4j output
Code:
2010-10-07 11:21:58 DEBUG [Configuration] hibernate.transaction.auto_close_session=true
(...)
2010-10-07 11:21:59 INFO [Configuration] Configured SessionFactory: null
(...)
2010-10-07 11:22:00 INFO [SettingsFactory] Automatic session close at end of transaction: enabled
(...)
2010-10-07 11:22:00 INFO [SessionFactoryImpl] building session factory
(...)
2010-10-07 11:22:02 DEBUG [SessionFactoryImpl] instantiated session factory
(...)
2010-10-07 11:22:05 DEBUG [SessionImpl] opened session at timestamp: 12864433248
(...)
2010-10-07 11:22:05 DEBUG [JDBCTransaction] committed JDBC Connection
2010-10-07 11:22:05 DEBUG [ConnectionManager] transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
2010-10-07 11:22:05 DEBUG [ConnectionManager] releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2010-10-07 11:22:05 DEBUG [ConnectionManager] transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
2010-10-07 11:22:05 DEBUG [SessionImpl] opened session at timestamp: 12864433256
(...)
2010-10-07 11:22:11 DEBUG [SessionImpl] opened session at timestamp: 12864433318
2010-10-07 11:22:11 DEBUG [JDBCTransaction] begin
2010-10-07 11:22:11 DEBUG [ConnectionManager] opening JDBC connection
2010-10-07 11:22:11 DEBUG [JDBCTransaction] current autocommit status: false
(...)
2010-10-07 11:22:11 DEBUG [SQL] select partenaire0_.logo as col_0_0_ from partenaire partenaire0_ where partenaire0_.id=?
2010-10-07 11:22:11 DEBUG [AbstractBatcher] about to open ResultSet (open ResultSets: 0, globally: 0)
2010-10-07 11:22:11 DEBUG [Loader] result row:
2010-10-07 11:22:11 DEBUG [AbstractBatcher] about to close ResultSet (open ResultSets: 1, globally: 1)
2010-10-07 11:22:11 DEBUG [AbstractBatcher] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2010-10-07 11:22:11 DEBUG [StatefulPersistenceContext] initializing non-lazy collections
2010-10-07 11:22:11 DEBUG [JDBCTransaction] commit
2010-10-07 11:22:11 DEBUG [JDBCTransaction] committed JDBC Connection
2010-10-07 11:22:11 DEBUG [ConnectionManager] transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
2010-10-07 11:22:11 DEBUG [ConnectionManager] releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2010-10-07 11:22:11 DEBUG [ConnectionManager] transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
(--end of log--)
According to Spring transactionManager, I don't have to explicitly close my session.
I also tested closing in a finally block, and got "Session is closed" from the second Dao call.
I think it can be a problem even from my Hibernate or Spring configuration.
Thanks for you help !