我将MySQL集成到Apache Felix中。首先,我使用bndtools生成MySQL捆绑包和c3p0捆绑包。然后将它们全部添加到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。请帮帮我.多谢了。
我懒得修改c3p0源代码并重新构建新的c3p0包版本:)。所以我尝试Apache DBCP包:
org.apache.servicemix.bundles.commons-pool-1.5.4
org.apache.servicemix.bundles.commons-dbcp-1.4.0
(dbcp needs pool to work)
对MySQL数据库进行CRUD是可以的。
如果有人想要使用这些包,这里有:
http://mvnrepository.com/artifact/org.apache.servicemix.bundles/org.apache.servicemix.bundles.commons-pool
http://mvnrepository.com/artifact/org.apache.servicemix.bundles/org.apache.servicemix.bundles.commons-dbcp
当我有时间的时候,如果有人已经为它构建了一个bundle,我会看一看bonecp。或修改c3p0以使用它。
问题内容: MySQL似乎在其连接上有8个小时的超时。我正在使用Hibernate for ORM在Tomcat中运行多个WAR。8小时(即通宵)后,当它拾取空闲连接时,管道破裂。 我已经遍历了代码,并确保我提交或回滚了所有事务。 这是我的hibernate.cfg.xml 我认为可以解决的参数是--默认为0。但是,运行8小时后,我们仍然遇到断管问题。尽管有多个通过Google发布的帖子索引,但没
我们正在运行一个带有Spring Hibernate MySql的J2EE应用程序,该应用程序在内部使用C3P0和Connector/J进行连池。 C3P0 C3P0配置 我的应用程序在一段时间后内部调用存储过程,我遇到以下错误 错误日志 现在,当我查看数据库日志时,我发现大多数查询都处于“创建排序索引”状态,如下所示 有人能帮我吗... 编辑 1.c3p0初始化日志
c3p0 是一个成熟的、高并发的 JDBC 连接池库,支持缓存和 PreparedStatements 的重用。它以LGPL v.2.1或EPL v.1.0授权。 详细的文档在C3P0的下载包中。本站提供最新版的C3P0文档,链接是: http://tool.oschina.net/apidocs/apidoc?api=c3p0
已更新 无法连接到postgres数据库。postgres的依赖关系是通过Maven添加的。 Maven依赖项 Postgres池 获取连接的代码 如果您有相同的问题,请将代码更改为:
我有一个Karaf计划,包含以下组件: Karaf 4.0.7; Hibernate4.3.7; MySQL JDBC连接器5.1.27; C3P0 0.9.5. 我刚刚建立了一个新的开发环境,我正在使用Mysql 8.0.11(最近才发布)。我已经在本地将Mysql Connector升级到5.1.46,这样它就可以与Mysql 8兼容,而且基本上可以正常工作。但是,我在Karaf控制台中收到一
我有一个使用Struts1.3、Hibernate3和Spring Security开发的web应用程序。由于MySQL在此时间之后关闭了连接,该应用程序在8小时后就停止了运行。然后我从几个帖子中收集信息,使它保持很长时间,现在差不多是20-24小时。有没有人能帮我把这东西弄好。 摘要 Iam在不活动20小时后无法登录。连接被MySQL关闭。 多谢了。 C3p0日志 INFO AbstractPo