Resin配置数据库连接池

时间:2021-09-09 13:37:39

首先需要将相应的JDBC驱动放在Resin主目录下的lib目录下
1
resin.conf文件中加入(sample)
<database>
        <jndi-name>jdbc/mysql</jndi-name>
        <driver type="org.gjt.mm.mysql.Driver">
         <url>jdbc:mysql://localhost:3306/mh</url>
         <user>mh81</user>
         <password>mh81</password>
        </driver>
        <prepared-statement-cache-size>8</prepared-statement-cache-size>
        <max-connections>20</max-connections>
        <max-idle-time>30s</max-idle-time>
</database>
2,web.xml
中加入

<resource-ref>
   <description>jdbc/mysql</description>
   <res-ref-name>jdbc/mysql</res-ref-name>
   <res-type>javax.sql.DataSource</res-type>
   <res-auth>Container</res-auth>
</resource-ref>
3,
代码中如何通过连接池获得数据库连接?
Context env = new InitialContext();
DataSource pool = (DataSource) env.lookup("java:comp/env/jdbc/mysql");
if (pool == null) {
    throw new Exception (dataSource + " is unknown datasource!");
}

conn = pool.getConnection();