【问题标题】:Tomcat DataSource - Connection PoolingTomcat 数据源 - 连接池
【发布时间】:2016-10-14 16:57:18
【问题描述】:

我正在为我的应用程序使用 tomcat 数据源连接池。我已将最大连接数设置为 20。我一次运行 5 个并发请求。但是,由于找不到可用的连接,我的一些呼叫超时。连接完成后返回池需要多长时间?有什么属性可以帮助解决这个问题吗?

我正在查看此 link 中的不同属性

【问题讨论】:

    标签: java spring apache tomcat connection-pooling


    【解决方案1】:

    默认连接超时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

    【讨论】:

    • 谢谢@Bhaskara Arani。执行数据库操作后,连接是否需要 60 秒才能返回池?你能帮忙指出参数吗?
    • 这里本身所有的参数都提到了,你要哪些参数额外p.setMaxActive(100);
    • 感谢 Bhaskara,在执行数据库操作后,连接是否需要 60 秒才能回到池中?
    • 不,这取决于您打开连接的程度,连接关闭后它将进入池
    猜你喜欢
    • 1970-01-01
    • 2015-12-26
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多