c3p0的使用
Posted lorentz-z
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c3p0的使用相关的知识,希望对你有一定的参考价值。
pom.xml
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.4</version> </dependency>
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!--默认配置--> <default-config> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> <property name="maxStatements">200</property> </default-config> <!--配置连接池mysql--> <named-config name="mysql"> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/library_ssm?serverTimezone=GMT%2B8</property> <property name="user">root</property> <property name="password">000000</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> <property name="maxStatements">200</property> </named-config> </c3p0-config>
C3p0Util
import com.mchange.v2.c3p0.ComboPooledDataSource; import org.apache.log4j.Logger; import java.sql.Connection; public class C3p0Util { private static Logger logger = Logger.getLogger(C3p0Util.class); private static ComboPooledDataSource cpds = new ComboPooledDataSource("mysql"); public static Connection getConnection(){ Connection conn=null; try { conn= cpds.getConnection(); logger.debug("从连接池获取了连接"); }catch (Exception e){ e.printStackTrace(); logger.error("获取连接异常"); } return conn; } public static void releaseConnection(Connection conn){ try{ if(conn!=null){ conn.close(); logger.debug("释放了一个连接"); } }catch (Exception e){ e.printStackTrace(); logger.error("释放连接异常"); } } }
以上是关于c3p0的使用的主要内容,如果未能解决你的问题,请参考以下文章