PDA

View Full Version : Hibernate Connection Pooling



amisra
Sep 24th, 2004, 11:57 AM
Hi,

Before I start describing my issue, I should mention that I am relatively new to spring ..

We are developing a spring + hibernate application. We have our application setup such that we do not explicitly manage transactions (we let spring manage it for us).

I am trying to configure connection pooling and towards this I have set the appropriate c3p0 properties (applicationcontext.xml pasted at the end of this mail). But everytime I issue a database command it seems to me that spring is creating a new connection... (Message from log pasted below)

Sep 24, 2004 11:00:20 AM org.springframework.jdbc.datasource.DriverManagerD ataSource getConnectionFromDriverManager
INFO: Creating new JDBC connection to [jdbc:oracle:thin:@dev.xyz.com:1521:dev1]

Why is a new JDBC connection being created each time ? What am I doing worng?

Best Regards
Ankur

Relevant sections of my applicationcontext.xml are pasted below
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFact oryBean">
<property name="dataSource"> <ref local="dataSource"/> </property>
<property name="mappingResources">
<list>
<value>com/mtgi/cciaws/model/User.hbm.xml</value>
<value>com/mtgi/cciaws/model/ResourceGroup.hbm.xml</value>
<value>com/mtgi/cciaws/model/Company.hbm.xml</value>
<value>com/mtgi/cciaws/model/Usersresgrpxref.hbm.xml</value>
<value>com/mtgi/cciaws/model/Product.hbm.xml</value>
<value>com/mtgi/cciaws/model/Productattributes.hbm.xml</value>
<value>com/mtgi/cciaws/model/Productdependencies.hbm.xml</value>
<value>com/mtgi/cciaws/model/Package.hbm.xml</value>
<value>com/mtgi/cciaws/model/Attribute.hbm.xml</value>
<value>com/mtgi/cciaws/model/Attributevalues.hbm.xml</value>
<value>com/mtgi/cciaws/model/Security.hbm.xml</value>
<value>com/mtgi/cciaws/model/Packageproductxref.hbm.xml</value>
<value>com/mtgi/cciaws/model/Transactionlog.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.c3p0.minPoolSize">5</prop>
<prop key="hibernate.c3p0.maxPoolSize">20</prop>
<prop key="hibernate.timeout">1800</prop>
<prop key="hibernate.max_statement">50</prop>
</props>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransac tionManager">
<property name="sessionFactory"><ref local="sessionFactory"/> </property>
</bean>

<!-- BEGIN User Bean Definition -->
<bean id="userDAO" class="com.mtgi.cciaws.dao.hibernate.UserDAOHibernate">
<property name="sessionFactory"><ref local="sessionFactory"/> </property>
</bean>

<bean id="userManagerTarget" class="com.mtgi.cciaws.service.impl.UserManagerImpl">
<property name="userDAO"><ref local="userDAO"/> </property>
<property name="validator"><ref bean="userValidator"/> </property>
</bean>

<bean id="userValidator" class="com.mtgi.cciaws.web.UserValidator"/>

<bean id="userManager" class="org.springframework.transaction.interceptor.Transa ctionProxyFactoryBean">
<property name="transactionManager"><ref local="transactionManager"/> </property>
<property name="target"><ref local="userManagerTarget"/> </property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- END User Bean Definition -->

</beans>

calle
Sep 24th, 2004, 12:53 PM
It seems to me you haven't defined your datasource anywhere, so I'm not even sure where your JDBC connections are coming from right now.

Wouldn't it be better to simply define a pooling DataSource (like DBCP, don't know whether C3P0 has one)?

Carl-Eric

amisra
Sep 24th, 2004, 02:39 PM
My apologies..

The database connection information is being read out of a properties files... The contents of the same are pasted below...

login.keyLocation=C:\\key.jks
interface.protocol=https://
interface.hostname=dev.xyz.com
interface.path=
interface.service=
jdbc.driverClassName=oracle.jdbc.driver.OracleDriv er
jdbc.url=jdbc:oracle:thin:@dev.xyz.com:1521:dev1
jdbc.jndiname=jdbc/oracleattws
jdbc.username=bogus_user
jdbc.password=bogus_password

Also, here is the portion of the applicationcontext.xml which shows the database connections...

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyP laceholderConfigurer">
<property name="location">
<value>/WEB-INF/classes/application.properties</value>
</property>
</bean>

<bean id="applicationProps" class="com.mtgi.cciaws.util.CCIGlobal">
<property name="keyLocation"> <value>${login.keyLocation}</value> </property>
<property name="protocol"> <value>${interface.protocol}</value> </property>
<property name="hostname"> <value>${interface.hostname}</value> </property>
<property name="path"> <value>${interface.path}</value> </property>
<property name="service"> <value>${interface.service}</value> </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerD ataSource">
<property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property>
<property name="url"> <value>${jdbc.url}</value> </property>
<property name="username"> <value>${jdbc.username}</value> </property>
<property name="password"> <value>${jdbc.password}</value> </property>
</bean>

irbouho
Sep 24th, 2004, 02:52 PM
But everytime I issue a database command it seems to me that spring is creating a new connection
Spring Transaction Management Layer uses a new connection each time a transaction starts and close it when the transaction ends. The connection may be a new one created using JDBC DriverManager or recycled from a DataSource.


Sep 24, 2004 11:00:20 AM org.springframework.jdbc.datasource.DriverManagerD ataSource getConnectionFromDriverManager
INFO: Creating new JDBC connection to [jdbc:oracle:thin:@dev.xyz.com:1521:dev1]
It seems that your are using Spring Framework DriverManagerDataSource, this class is an implementation of SmartDataSource that is sweetable for test and development environment, not for production.