我使用MysqlConnectionPoolDataSource。我不得不从jndi定义迁移到自定义解决方案。旧的定义看起来像这样:
<data-source
name="MySqlDS" location="jdbc/MySqlPooledDS"
class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"
max-connections="100"
min-connections="5"
inactivity-timeout="200"
wait-timeout="10"
username="USERNAME"
password="PASSWORD"
url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=Cp1251"
/>我想用下面的代码来代替它:
DataSource dataSource = (DataSource) Class.forName(driver.getType()).newInstance();
new Statement(dataSource, "setUrl", new Object[]{ driver.getUrl().getValue() }).execute();
new Statement(dataSource, "setUser", new Object[]{ driver.getUser().getValue() }).execute();
new Statement(dataSource, "setPassword", new Object[]{ driver.getPassword().getValue() }).execute();
new Statement(dataSource, "setUseUnicode", new Object[]{ "yes".equals(driver.getUseUnicode().getValue()) }).execute();
new Statement(dataSource, "setCharacterEncoding", new Object[]{ driver.getCharacterEncoding().getValue() }).execute();我想在dataSource上定义max-connections属性,但是我不知道怎么做。有什么想法吗?
谢谢。
发布于 2013-11-29 20:07:37
您必须将其放在连接字符串中:"jdbc:mysql://localhost:3306/test?max-connections=100..."
发布于 2013-11-29 20:06:49
你的替换代码有点奇怪,为什么不使用一个具体的数据源实现呢?然后,您可以使用适当的setter直接设置url、user等。
我建议使用像c3p0或BoneCP这样的东西来设置一个连接池,对于MySQL来说,这样做没有太大的麻烦。
BoneCP
c3p0
https://stackoverflow.com/questions/20285279
复制相似问题