简体   繁体   English

karaf + pax-jdbc连接池已达到限制

[英]karaf + pax-jdbc the connection pool had reached the limit

I have a problem with the pool connections of pax-jdbc in karaf, I'm trying to inject a Mysql DataSource (DS) through blueprint.xml into my project, for test it, I have built a karaf command where injects the DS into karaf command class and execute a query with that connection. 我在karaf中的pax-jdbc的池连接有问题,我试图通过blueprint.xml将Mysql数据源(DS)注入到我的项目中,以进行测试,我建立了一个karaf命令,将DS注入到karaf命令类,并使用该连接执行查询。 That it's OK, but the problem is when I execute the command a lot times, for each execution a new instance of the DS is created and the pool connection cannot open new connections to MySQL, because the pool had reached the limit. 没关系,但是问题是当我多次执行命令时,每次执行都会创建一个新的DS实例,并且池连接无法打开与MySQL的新连接,因为池已达到限制。

I have uploaded my code to github in this link: https://github.com/christmo/karaf-pax-jdbc , you can give a pull request if you find an error in this project. 我已通过以下链接将代码上传到github: https : //github.com/christmo/karaf-pax-jdbc ,如果在此项目中发现错误,则可以发出拉取请求。

For test this project you can do: 为了测试该项目,您可以执行以下操作:

1. Download karaf 4.0.4 or apache-karaf-4.1.0-SNAPSHOT
2. Copy the file karaf-pax-jdbc/etc/org.ops4j.datasource-my-ds.cfg to ${karaf}/etc, this file have the mysql 
   configuration change with your mysql configuration data.
4. Start mysql database engine
3. Start karaf -> cd ${karaf}/bin/; ./karaf
4. Add the repo of this project with this karaf command: feature:repo-add mvn:pax/features/1.0-SNAPSHOT/xml/features
5. Install the feature created for this project: feature:install mysql-test
6. Execute the command for test this problem: mysql-connection, this command only execute "Select 1" in mysql

If you execute 9 times this command "mysql-connection", it will freeze the prompt of karaf and if you interrupt the execution you can get this exception: 如果您执行9次此命令“ mysql-connection”,它将冻结karaf的提示,如果中断执行,您将得到以下异常:

java.sql.SQLException: Cannot get a connection, general error at org.apache.commons.dbcp2.PoolingDataSource.getConnection(PoolingDataSource.java:146) at com.twim.OrmCommand.execute(OrmCommand.java:53) at org.apache.karaf.shell.impl.action.command.ActionCommand.execute(ActionCommand.java:83) at org.apache.karaf.shell.impl.console.osgi.secured.SecuredCommand.execute(SecuredCommand.java:67) at org.apache.karaf.shell.impl.console.osgi.secured.SecuredCommand.execute(SecuredCommand.java:87) at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:480) at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:406) at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108) at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:182) at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:119) at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:94) at org.apache.karaf.shell.impl.console.ConsoleSessionImpl.run(ConsoleSessionImpl.java java.sql.SQLException:无法获得连接,在org.com.twim.OrmCommand.execute(OrmCommand.java:53)处的org.apache.commons.dbcp2.PoolingDataSource.getConnection(PoolingDataSource.java:146)处出现一般错误。 org.apache.karaf.shell.impl.console.osgi.secured.SecuredCommand.execute(SecuredCommand.java:67)处的apache.karaf.shell.impl.action.command.ActionCommand.execute(ActionCommand.java:83) org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:480)上的org.apache.karaf.shell.impl.console.osgi.secured.SecuredCommand.execute(SecuredCommand.java:87)在org.apache.felix.gogo.runtime的apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:406)在org.apache.felix.gogo.runtime的Pipe.run(Pipe.java:108)。 org.apache.felix.gogo.runtime的Closure.execute(Closure.java:182).org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java)的Closure.execute(Closure.java:119): 94)在org.apache.karaf.shell.impl.console.ConsoleSessionImpl.run(ConsoleSessionImpl.java :270) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.InterruptedException at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:2014) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2048) at org.apache.commons.pool2.impl.LinkedBlockingDeque.takeFirst(LinkedBlockingDeque.java:583) at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:442) at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363) at org.apache.commons.dbcp2.PoolingDataSource.getConnection(PoolingDataSource.java:134) ... 12 more :270)at java.lang.Thread.run(Thread.java:745)造成原因:java.java.util.concurrent.locks.AbstractQueuedSynchronizer $ ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:2014)处的java.lang.InterruptedException。 org.apache.commons.pool2.impl.LinkedBlockingDeque.takeFirst(LinkedBlockingDeque.java:583)上的util.concurrent.locks.AbstractQueuedSynchronizer $ ConditionObject.await(AbstractQueuedSynchronizer.java:2048)在org.apache.commons.pool2.impl上。 org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)的GenericObjectPool.borrowObject(GenericObjectPool.java:442)org.apache.commons.dbcp2.PoolingDataSource.getConnection(PoolingDataSource.java:134) ...另外12个

The problem in your code is in the line System.out.println("--DS--: " + ds.getConnection()); 您的代码中的问题在System.out.println("--DS--: " + ds.getConnection()); .

There you create a connection but never close it. 在此创建连接,但永远不要关闭它。 So with every call you drain the pool. 因此,每次通话都会耗尽池水。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM