一、问题描述
在c3p0配置过程中,遇到如下异常:
java.sql.SQLException: Connections could not be acquired from the underlying database
此异常为c3p0配置异常,c3p0.xml文件配置没有配置对!
初始配置如下:
<!-- 指定连接数据源的基本属性 -->
<property name="user">root</property>
<property name="password">123</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC</property>
用此配置文件运行后会弹出:
且c3p0数据池不能成功连接。
二、如何解决
在 c3p0.xml 文件中对于 ‘&’ 需要转义成 ‘&;’!!
即在 & 后需要加上 amp;
代码如下:
<!-- 指定连接数据源的基本属性 -->
<property name="user">root</property>
<property name="password">123</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC</property>
再运行,c3p0 连接成功!为了验证成功连接,增加打印语句
System.out.println(connection);
成功连接!