Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection Pooling with Spring & Hibernate

How to configure connection pooling with Spring and Hibernate?

Thanks

Venu

like image 540
user678936 Avatar asked Apr 03 '11 06:04

user678936


People also ask

What is connection pooling?

Connection pooling means that connections are reused rather than created each time a connection is requested. To facilitate connection reuse, a memory cache of database connections, called a connection pool, is maintained by a connection pooling module as a layer on top of any standard JDBC driver product.

When should you not use connection pooling?

You reuse a prior database connection, in a new context to avoid the cost of setting up a new database connection for each request. The primary reason to avoid using database connections is that you're application's approach to solving problems isn't structured to accommodate a database connection pool.

Which is the best connection pooling in Java?

Although there are a lot of frameworks available to choose from like C3P0, Apache DBCP, BoneCp, Vibur etc. However, the most popular choices are Tomcat JDBC & HikariCP.

How do you implement a connection pool?

Let's have a look at below steps to initialize connection pool: Create an instance of ConnectionFactory using JDBC URL. Create an instance of PoolableConnectionFactory using an instance of ConnectionFactory which was created in step 1.


1 Answers

You can use DBCP component

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">

        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="initialSize" value="10" />
        <property name="maxActive" value="5" />
        <property name="maxWait" value="5000" />
    </bean>


    <!-- Hibernate Configuration -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
        p:dataSource-ref="dataSource">
        <property name="annotatedClasses">
            <list>
                <value>com.project.domain.Domain1</value>
                <value>com.project.domain.Domain1</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    ${hibernate.dialect}
                </prop>
                <prop key="hibernate.show_sql">
                    ${hibernate.show_sql}
                </prop>
                <prop key="hibernate.generate_statistics">
                    ${hibernate.show_statistics}
                </prop>
            </props>
        </property>
    </bean>
like image 119
Adi Sembiring Avatar answered Sep 22 '22 16:09

Adi Sembiring