Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the proper way to handle JDBC connections with Spring and DBCP?
    primarykey
    data
    text
    <p>I'm using the Spring MVC to build a thin layer on top of a SQL Server database. When I began testing, it seems that it doesn't handle stress very well :). I'm using <a href="http://commons.apache.org/dbcp/" rel="nofollow noreferrer">Apache Commons DBCP</a> to handle connection pooling and the data source. </p> <p>When I first attempted ~10-15 simultaneous connections, it used to hang and I'd have to restart the server (for dev I'm using Tomcat, but I'm gonna have to deploy on Weblogic eventually).</p> <p>These are my Spring bean definitions:</p> <pre><code>&lt;bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"&gt; &lt;property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/&gt; &lt;property name="url" value="[...]"/&gt; &lt;property name="username" value="[...]" /&gt; &lt;property name="password" value="[...]" /&gt; &lt;/bean&gt; &lt;bean id="partnerDAO" class="com.hp.gpl.JdbcPartnerDAO"&gt; &lt;constructor-arg ref="dataSource"/&gt; &lt;/bean&gt; &lt;!-- + other beans --&gt; </code></pre> <p>And this is how I use them:</p> <pre><code>// in the DAO public JdbcPartnerDAO(DataSource dataSource) { jdbcTemplate = new JdbcTemplate(dataSource); } // in the controller @Autowired private PartnerDAO partnerDAO; // in the controller method Collection&lt;Partner&gt; partners = partnerDAO.getPartners(...); </code></pre> <p>After reading around a little bit, I found the <code>maxWait</code>, <code>maxActive</code> and <code>maxIdle</code> properties for the <a href="https://commons.apache.org/proper/commons-dbcp/api-1.4/org/apache/commons/dbcp/BasicDataSource.html" rel="nofollow noreferrer">BasicDataSource</a> (from <a href="https://commons.apache.org/proper/commons-pool/api-1.6/org/apache/commons/pool/impl/GenericObjectPool.html" rel="nofollow noreferrer">GenericObjectPool</a>). Here comes the problem. I'm not sure how I should set them, performance-wise. From what I know, Spring should be managing my connections so I shouldn't have to worry about releasing them. </p> <pre><code>&lt;bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"&gt; &lt;property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/&gt; &lt;property name="url" value="[...]"/&gt; &lt;property name="username" value="[...]" /&gt; &lt;property name="password" value="[...]" /&gt; &lt;property name="maxWait" value="30" /&gt; &lt;property name="maxIdle" value="-1" /&gt; &lt;property name="maxActive" value="-1" /&gt; &lt;/bean&gt; </code></pre> <p>First, I set <code>maxWait</code>, so that it wouldn't hang and instead throw an exception when no connection was available from the pool. The exception message was:</p> <blockquote> <p>Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object</p> </blockquote> <p>There are some long-running queries, but the exception was thrown regardless of the query complexity.</p> <p>Then, I set maxActive and maxIdle so that it wouldn't throw the exceptions in the first place. The default values are 8 for <code>maxActive</code> and <code>maxIdle</code> (I don't understand why); if I set them to -1 there are no more exceptions thrown and everything <em>seems</em> to work fine.</p> <p>Considering that this app should support a large number of concurrent requests is it ok to leave these settings to infinite? Will Spring actually manage my connections, considering the errors I was receiving? Should I switch to <a href="http://sourceforge.net/projects/c3p0/" rel="nofollow noreferrer">C3P0</a> considering it's kinda dead?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload