Tomcat ConnectionPooling 使用

在tomcat的conf目录下:
  context.xml
<Resource name="JNDI/iptv" type="javax.sql.DataSource"
removeAbandonedTimeout="30"
maxActive="30"
maxIdle="5" maxWait="5000"
driverClassName="com.mysql.jdbc.Driver"
username="root"  password="root!"
url="jdbc:mysql://localhost:3305/iptv" />

web.xml
<resource-ref>
  <description>DB Connection Pooling</description>
  <res-ref-name>JNDI/iptv</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>
(以上都是最外层的里面加上)

将对应的JDBC diver 拷贝到lib 目录下。


然后可以用

以下代码使用tomcat connectionPooling :
public Connection getConnection() {
        try {
            InitialContext initContext = new InitialContext();
            Context envContext = (Context) initContext.lookup("java:/comp/env");
            DataSource ds;
            ds = (DataSource) envContext.lookup("jdbc/mysql");
            conn = ds.getConnection();
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

你可能感兴趣的:(sql,tomcat,mysql,xml,jdbc)