Tomcat DataSource - 连接池

时间:2016-10-14 16:57:19

标签: java spring apache tomcat connection-pooling

我正在为我的应用程序使用tomcat数据源连接池。我已将最大连接数设置为20.我一次运行5个并发请求。但是我的一些调用是超时的,因为找不到可用的连接。一旦完成连接回到池中需要多长时间?是否有任何有助于此的属性?

我在查看此link

中的不同属性

1 个答案:

答案 0 :(得分:2)

默认连接超时60秒。

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;

public class SimplePOJOExample {

  public static void main(String[] args) throws Exception {
      PoolProperties p = new PoolProperties();
      p.setUrl("jdbc:mysql://localhost:3306/mysql");
      p.setDriverClassName("com.mysql.jdbc.Driver");
      p.setUsername("root");
      p.setPassword("password");
      p.setJmxEnabled(true);
      p.setTestWhileIdle(false);
      p.setTestOnBorrow(true);
      p.setValidationQuery("SELECT 1");
      p.setTestOnReturn(false);
      p.setValidationInterval(30000);
      p.setTimeBetweenEvictionRunsMillis(30000);
      p.setMaxActive(100);
      p.setInitialSize(10);
      p.setMaxWait(10000);
      p.setRemoveAbandonedTimeout(60);
      p.setMinEvictableIdleTimeMillis(30000);
      p.setMinIdle(10);
      p.setLogAbandoned(true);
      p.setRemoveAbandoned(true);
      p.setJdbcInterceptors(
        "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
        "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
      DataSource datasource = new DataSource();
      datasource.setPoolProperties(p);

      Connection con = null;
      try {
        con = datasource.getConnection();
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from user");
        int cnt = 1;
        while (rs.next()) {
            System.out.println((cnt++)+". Host:" +rs.getString("Host")+
              " User:"+rs.getString("User")+" Password:"+rs.getString("Password"));
        }
        rs.close();
        st.close();
      } finally {
        if (con!=null) try {con.close();}catch (Exception ignore) {}
      }
  }

}

以下是有关如何为JNDI查找配置资源的示例

<Resource name="jdbc/TestDB"
      auth="Container"
      type="javax.sql.DataSource"
      factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
      testWhileIdle="true"
      testOnBorrow="true"
      testOnReturn="false"
      validationQuery="SELECT 1"
      validationInterval="30000"
      timeBetweenEvictionRunsMillis="30000"
      maxActive="100"
      minIdle="10"
      maxWait="10000"
      initialSize="10"
      removeAbandonedTimeout="60"
      removeAbandoned="true"
      logAbandoned="true"
      minEvictableIdleTimeMillis="30000"
      jmxEnabled="true"
      jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;
        org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
      username="root"
      password="password"
      driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://localhost:3306/mysql"/>

如果您发现任何困难,请参阅此处 https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html