1

I have a problem integrating Spring, Hibernate, and Apache DBCP. I have used the DBCPConnectionProvider from here.

I have the following xml configuration for the above said.

<context:component-scan base-package="com.my.app"/>

<tx:annotation-driven/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="packagesToScan">
        <list>
            <value>com.my.app.model</value>
        </list>
    </property>

    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>

            <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
            <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/app</prop>
            <prop key="hibernate.connection.username">foo</prop>
            <prop key="hibernate.connection.password">bar</prop>
            <prop key="hibernate.connection.provider_class">org.hibernate.connection.DBCPConnectionProvider</prop>

            <!-- Connection pooling related properties -->

            <prop key="hibernate.dbcp.initialSize">8</prop>
            <prop key="hibernate.dbcp.maxActive">20</prop>
            <prop key="hibernate.dbcp.maxIdle">5</prop>
            <prop key="hibernate.dbcp.minIdle">0</prop>
            <prop key="hibernate.dbcp.maxWait">10000</prop>
            <prop key="hibernate.dbcp.minEvictableIdleTimeMillis">180000</prop>
            <prop key="hibernate.dbcp.timeBetweenEvictionRunsMillis">180000</prop>
            <prop key="hibernate.dbcp.testWhileIdle">true</prop>
            <prop key="hibernate.dbcp.testOnReturn">true</prop>
            <prop key="hibernate.dbcp.validationQuery">select 1</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

When the database schema i.e. app is empty two tables will be created in mysql. But I am getting NullPointerException in the getConnection() method of the DBCPConnectionProvider that I mentioned. That means the dataSource is not instantiated. I think I have configured everything in the xml. What am I missing. How do you configure DBCP version 1.4 with Spring and Hibernate? Please provide your insights.

Here is the stack trace:

Caused by: java.lang.NullPointerException
  at org.hibernate.connection.DBCPConnectionProvider.getConnection(DBCPConnectionProvider.java:209)
  at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
  at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
  at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
  at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
  at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
  at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:555)
  ... 82 more

1 Answer 1

5

Don't do it like that. Configure the datasource you want to use in Spring as well as Hibernate. Ditch the hibernate.dbcp and hibernate.connection properties.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/app"/>
    <property name="username" value="foo"/>
    <property name="password" value="bar"/>
    // Other DBCP properties here
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource"  ref="dataSource"   
    <property name="packagesToScan">
        <list>
            <value>com.my.app.model</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

Just add the dataSource property to your AnnotationSessionFactoryBean as dependency and done. Note you don't need the configurationClass property as it is already annotation based.

A tip I wouldn't suggest using Commons-DBCP anymore as a datasource instead take a look at HikariCP as a better datasource implementation.

For more information in integrating/configuring Hibernate with Spring I suggest this section of the Reference Guide.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you. But, I heard that BasicDataSource aka dbcp directly is not supported by hibernate. So, we need to have a class that implements ConnectionProvider interface. Am I wrong?
No you don't. You can use whatever datasource you like... You don't need the connection provider. Also using the internal pooling of hibernate isn't recommended for use (as they state in their own documentation). Just inject the datasource and that is all you need.
@M.Denium Okay. I tried the config. But, I see not 8 connections in the mysql workbench when the app is started. 2 are already there by default. I was expecting 2 + 8 connections when the app starts. But the number is something else.
@M.Denium I see only 5 connections created. What does it mean? This was one of the reasons why I was using the above mentioned class.
Make sure you added allt he properties if you copied the code above you only get the default commons-dbcp settings (which might be 5).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.