当前位置: 首页 > 工具软件 > Apache Felix > 使用案例 >

java c3p0 mysql_MySQL,c3p0和Apache Felix集成

祁默
2023-12-01

我将MySQL集成到Apache Felix中 . 首先,我使用bndtools生成MySQL bundle和c3p0 bundle . 然后我将它们全部添加到我的Apache Felix环境中 . 我为连接池创建了一个类,如下所示:

public final class C3P0Manager {

private static C3P0Manager instance;

private DataSource pooled;

private C3P0Manager() {

// Of course, it is better to put all properties into a configuration file.

// I just list them here for easy reading.

ComboPooledDataSource cpds = new ComboPooledDataSource();

cpds.setDriverClass("com.mysql.jdbc.Driver"));

cpds.setJdbcUrl("jdbc:mysql://localhost/my-database?autoReconnect=true&characterSetResults=UTF-8&characterEncoding=UTF-8&useUnicode=yes");

cpds.setUser("user");

cpds.setPassword("password");

cpds.setInitialPoolSize(3);

cpds.setMaxPoolSize(15);

cpds.setMaxIdleTime(1800);

cpds.setAutoCommitOnClose(true);

pooled(cpds);

}

public static C3P0Manager instance() throws Exception {

if (instance == null) {

instance = new C3P0Manager();

}

return instance;

}

public DataSource getPooled() throws SQLException {

return pooled;

}

}

如果我运行JUnit测试,它工作正常 . 但是在我的Apache Felix捆绑包上运行异常消息时失败了 . 在Activator类中的用法:

Class.forName("com.mysql.jdbc.Driver");

DataSource pooled = C3P0Manager.instance().getPooled();

Connection con = pooled.getConnection();

PreparedStatement stmt = null;

ResultSet rs = null;

int total;

try {

stmt = con.prepareStatement("SELECT count(*) FROM users", Statement.NO_GENERATED_KEYS);

rs = stmt.executeQuery();

if (rs.next()) {

total = rs.getInt(1);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

rs.close();

stmt.close();

} catch (Exception ex) {

ex.printStackTrace();

}

}

System.out.println("total = " + total);

错误消息:

java.sql.SQLException: Connections could not be acquired from the underlying database!

at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)

at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)

at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)

...

Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.

at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)

at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)

at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)

at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)

...

如果我只使用(没有c3p0),MySQL可以工作:

Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost/my-database?autoReconnect=true&characterSetResults=UTF-8&characterEncoding=UTF-8&useUnicode=yes","user","password");

Statement statement = connect.createStatement();

ResultSet resultSet = statement.executeQuery("SELECT count(*) FROM users");

所以我认为问题是由于c3p0 . 请帮我 . 谢谢 .

 类似资料: