限制Play Framework中创建的服务器数量(EbeanServerFactory.createServer)?

时间:2015-01-06 10:09:44

标签: java jdbc playframework-2.0 ebean bonecp

我在使用play框架开发时遇到了一个奇怪的问题。我的代码动态创建一个带有关联数据源配置的服务器。如果我重复“服务器创建”操作75次,那么我就会包含“太多连接问题”。

那么,是否可以更改此限制,或者是否有一种方法可以“删除/取消注册”使用EbeanServerFactory.createServer方法创建的服务器? THX

这是我创建服务器的代码:

ServerConfig serverConfig = new ServerConfig();
DataSourceConfig dataSourceConfig = new DataSourceConfig();
String driver = "com.mysql.jdbc.Driver";
dataSourceConfig.setDriver(driver);
// Ensure that driver is properly loaded
try {
    Class.forName(driver).newInstance();
} catch (InstantiationException | IllegalAccessException
        | ClassNotFoundException e) {
    System.out.println("Can't load database driver: "+driver+" because  +Throwables.getStackTraceAsString(e));
    System.exit(0);
}
String dbUrl = "jdbc:mysql://192.168.0.2/mydatabase?characterEncoding=UTF-8";
String user = "myuser";
String password = "mypasswd";
dataSourceConfig.setUrl(dbUrl);
dataSourceConfig.setUsername(user);
dataSourceConfig.setPassword(password);
dataSourceConfig.setCaptureStackTrace(true);
serverConfig.setDataSourceConfig(dataSourceConfig);
serverConfig.setName("test");
serverConfig.setDefaultServer(false);
serverConfig.setDdlGenerate(true);
serverConfig.setDdlRun(true);
serverConfig.setRegister(true);
try {
    EbeanServerFactory.create(serverConfig);
} catch (Exception e) {
    System.out.println("Can't create server "+serverConfig.getName()+" "+Throwables.getStackTraceAsString(e));
}

这是第75次迭代74次后的错误:

[error] c.a.e.s.l.s.DataSourcePool - FATAL: DataSourcePool [test] is down!!!
Can't create server test com.avaje.ebeaninternal.server.lib.sql.DataSourceException: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"
        at com.avaje.ebeaninternal.server.lib.sql.DataSourcePool.<init>(DataSourcePool.java:209)
        at com.avaje.ebeaninternal.server.core.DefaultServerFactory.getDataSourceFromConfig(DefaultServerFactory.java:420)
        at com.avaje.ebeaninternal.server.core.DefaultServerFactory.setDataSource(DefaultServerFactory.java:380)
        at com.avaje.ebeaninternal.server.core.DefaultServerFactory.createServer(DefaultServerFactory.java:163)
        at com.avaje.ebeaninternal.server.core.DefaultServerFactory.createServer(DefaultServerFactory.java:65)
        at com.avaje.ebean.EbeanServerFactory.create(EbeanServerFactory.java:58)
        at controllers.test.MyTest.ptest(MyTest.java:247)
        at Routes$$anonfun$routes$1$$anonfun$applyOrElse$203$$anonfun$apply$203.apply(routes_routing.scala:2473)
        at Routes$$anonfun$routes$1$$anonfun$applyOrElse$203$$anonfun$apply$203.apply(routes_routing.scala:2473)
        at play.core.Router$HandlerInvoker$$anon$7$$anon$2.invocation(Router.scala:183)
        at play.core.Router$Routes$$anon$1.invocation(Router.scala:377)
        at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:56)
        at play.GlobalSettings$1.call(GlobalSettings.java:64)
        at play.core.j.JavaAction$$anon$3.apply(JavaAction.scala:91)
        at play.core.j.JavaAction$$anon$3.apply(JavaAction.scala:90)
        at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82)
        at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82)
        at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:251)
        at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:249)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
        at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:37)
        at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:42)
        at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:386)
        at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
        at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
        at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
        at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

1 个答案:

答案 0 :(得分:0)

发现它......

在此代码中:

try {
    EbeanServerFactory.create(serverConfig);
} catch (Exception e) {
    System.out.println("Can't create server "+serverConfig.getName()+" "+Throwables.getStackTraceAsString(e));
}

使用以下方式获取创建EbeanServer:

    EbeanServer server=null;
try {
        server = EbeanServerFactory.create(serverConfig);
    } catch (Exception e) {
        System.out.println("Can't create server "+serverConfig.getName()+" "+Throwables.getStackTraceAsString(e));
    }

然后当服务器未使用时,只需执行:

server.shutdown (...,...)

...取决于你使用底层数据源和/或JDBC驱动程序的方式