Announcement Announcement Module
Collapse
No announcement yet.
Connection pooling Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Connection pooling

    Hi,

    Can any one point me to a good connection pooling example please. I am using spring, hibernate with tomcat. Currently i have the dbcp basic data source as follows:

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName"><value>com.mysql.jdbc.Drive r</value></property>
    <property name="url"><value>jdbc:mysql://localhost:3306/medinatravels</value></property>
    <property name="username"><value>root</value></property>
    <property name="password"><value></value></property>
    </bean>

    and reference this in my seesion factory definition

    But want to add connection pooling!!

    Thanks

  • #2
    If I create a resourcein my tomcat server.xml of type datasource as follows:

    <Resource name="jdbc/pool" scope="Shareable" type="javax.sql.DataSource"/>
    <ResourceParams name="jdbc/pool">
    <parameter>
    <name>factory</name>
    <value>org.apache.commons.dbcp.BasicDataSourceFact ory</value>
    </parameter>

    <!-- DBCP database connection settings -->
    <parameter>
    <name>url</name>
    <value>jdbc:mysql://localhost:3306/medinatravels</value>
    </parameter>
    <parameter>
    <name>driverClassName</name>
    <value>com.mysql.jdbc.Driver</value>
    </parameter>
    <parameter>
    <name>username</name>
    <value>root</value>
    </parameter>
    <parameter>
    <name>password</name>
    <value></value>
    </parameter>

    <!-- DBCP connection pooling options -->
    <parameter>
    <name>maxWait</name>
    <value>3000</value>
    </parameter>
    <parameter>
    <name>maxIdle</name>
    <value>100</value>
    </parameter>
    <parameter>
    <name>maxActive</name>
    <value>10</value>
    </parameter>
    </ResourceParams>

    Then I reference this in my spring xml as follows:

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName" value="java:comp/env/jdbc/pool"/>
    </bean>


    Does this mean I have connection pooling? How can I check?

    Comment


    • #3
      I'm not entirely sure what you're asking. If you use org.apache.commons.dbcp.BasicDataSource then, as I understand it, you have connection pooling.

      If you only want to access your dataSource via Spring, then what you're doing on your first post is all you need.

      The only benefit, as far as I can see, of doing what you suggest in your second post, is that you have now made your dataSource available via JNDI. It seems to me you'd only need to do that if you want it to be available somewhere that can't get it from your Spring context. Or if you want to use a single dataSource accross multiple applications.

      HTH

      Comment


      • #4
        Chris is correct. The Commons DBCP DataSource is a connection pool. I would recommend it as a simpler approach to using a server datasource. Unless you are using a full-blown application server with a JTA-capable transaction pool.

        Comment


        • #5
          Would I just set the parameters for size of pool, idel connections etc in the spring context then?

          Comment


          • #6
            Just set the relevant properties on BasicDataSource, in your Spring bean definition. Please refer to the Javadoc for BasicDataSource.

            Comment


            • #7
              Interesting point. Before we started using Spring we had our app setup using a JNDI pooled db connection source (Tomcat context.xml). Then we started using Spring, now one of our app's dao's accesses a networked resource where pooling connections is important and we ended up configuring it in Spring.

              Works nice however we were thinking: shouldn't we do it like we did it for the db connection pool, i.e. sticking it in JNDI via context.xml, to be consistent?

              Turns out that instead of putting that connection pool in Tomcat's context, we might as well configure the pooled db connection in Spring's configuration, have it all in one place. Nice and consistent.

              Thanks for the insight

              Comment

              Working...
              X