0
我在使用遊戲框架進行開發時遇到了一個奇怪的問題。我的代碼動態創建一個具有關聯數據源配置的服務器。如果我再次重複「服務器創建」操作75次,則會導致「連接問題太多」。限制Play Framework中創建的服務器數量(EbeanServerFactory.createServer)?
那麼,這個限制可以改變嗎?或者有沒有辦法使用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));
}
而這裏的後74次迭代的第75一個錯誤:
[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)