mysql jdbc源码分析片段 和 Tomcat's JDBC Pool

Posted maxlife

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql jdbc源码分析片段 和 Tomcat's JDBC Pool相关的知识,希望对你有一定的参考价值。

32) Tomcat‘s JDBC Pool

Tomcat jdbc pool的使用仅需2个jar包,分别为tomcat-jdbc.jar和tomcat-juli.jar,这两个jar包都可以在tomcat7中找到,tomcat-jdbc.jar在tomcat的lib目录下,tomcat-juli.jar在bin目录下。
http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/
org.apache.tomcat.jdbc.pool


  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) {}
          }
      }
  }

ProxyConnection
ProxyConnection
PooledConnection con = idle.poll();
ProxyConnection[PooledConnection[null]]

00 00 00 00 03 73 65 6c     . . . . . s e l
65 63 74 20 2a 20 46 52     e c t . * . F R
4f 4d 20 74 63 5f 63 6f     O M . t c _ c o
64 65 20 57 48 45 52 45     d e . W H E R E
20 43 4f 44 45 5f 49 44     . C O D E _ I D
3d 27 31 30 30 35 31 30     = ‘ 1 0 0 5 1 0 0 2 ‘
30 32 27

EscapeProcessor

Object escapedSqlResult = EscapeProcessor.escapeSQL(sql, serverSupportsConvertFn(), getMultiHostSafeProxy());
 EscapeTokenizer escapeTokenizer = new EscapeTokenizer(sql);
[49, 48, 48, 53, 49, 48, 48, 50]
[39, 49, 48, 48, 53, 49, 48, 48, 50, 39]
[39, 49, 48, 48, 53, 49, 48, 48, 50, 39]
[[39, 49, 48, 48, 53, 49, 48, 48, 50, 39]]

Buffer sendPacket = fillSendPacket();

Buffer sendPacket = this.connection.getIO().getSharedSendPacket();
this.position = MysqlIO.HEADER_LENGTH;
this.results = executeInternal(this.maxRows, sendPacket, createStreamingResultSet(), true, metadataFromCache, false);
Buffer getSharedSendPacket() {
    if (this.sharedSendPacket == null) {
        this.sharedSendPacket = new Buffer(INITIAL_PACKET_SIZE);
    }
    return this.sharedSendPacket;
}

protected MySQLConnection checkClosed() throws SQLException {
    MySQLConnection c = this.connection;

    if (c == null) {
        throw SQLError.createSQLException(Messages.getString("Statement.49"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
    }

    return c;
}

public ResultSetInternalMethods execSQL(StatementImpl callingStatement, String sql, int maxRows, Buffer packet, int resultSetType, int resultSetConcurrency,
            boolean streamResults, String catalog, Field[] cachedMetadata, boolean isBatch) throws SQLException


 return this.io.sqlQueryDirect(callingStatement, null, null, packet, maxRows, resultSetType, resultSetConcurrency, streamResults, catalog,
                        cachedMetadata);

StatementImpl

Buffer resultPacket = sendCommand(MysqlDefs.QUERY, null, queryPacket, false, null, 0);

  

以上是关于mysql jdbc源码分析片段 和 Tomcat's JDBC Pool的主要内容,如果未能解决你的问题,请参考以下文章

Tomcat JDBC Pool 源码实现简单分析

JDBC源码分析(加载过程)

jdbc-mysql测试例子和源码详解

tomcat 线程数与 mysql 连接数综合调优

无法加载 JDBC 驱动程序类“com.mysql.jdbc.Driver”Tomcat 8 和 Eclipse

Tomcat-JDBC源码解析及优化