Play测试中断了Ebean关闭

时间:2016-03-31 17:35:12

标签: java playframework-2.2 ebean

我们在Play服务器中使用基本测试制作了测试结构,我们在其中清理内存数据库,然后进行演变。代码的简单快照如下:

public static void ddlConfiguration() {
    String serverName = "default";
    server = Ebean.getServer(serverName);
    ServerConfig config = new ServerConfig();
    ddl = new DdlGenerator();

    // Get the database engine
    String jdbc = settings.get("db.default.url");
    String[] splittedUrl = jdbc.split(":");
    String engine = splittedUrl[1];
    if(engine.equals("h2")){
        ddl.setup((SpiEbeanServer) server, new H2Platform(), config);
    }else{
        ddl.setup((SpiEbeanServer) server, new MySqlPlatform(), config);
    }

}
@Before
public void startApp() throws IOException {
    app = Helpers.fakeApplication(settings);
    Helpers.start(app);

    ddlConfiguration();
} 
@After
public void stopApp() {
    // We clean the database at the end of the test in order not to overwrite
    // the onStart App initialization
    cleanDb();
    Helpers.stop(app);
}

问题是一旦执行所有测试并且Play服务器关闭,就会抛出如下异常:

[error] c.a.e.s.c.DefaultServer - Error unregistering Ebean Ebean:server=default2
javax.management.InstanceNotFoundException: Ebean:server=default2,key=AutoFetch
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1095) ~[na:1.8.0_72]
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.exclusiveUnregisterMBean(DefaultMBeanServerInterceptor.java:427) ~[na:1.8.0_72]
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.unregisterMBean(DefaultMBeanServerInterceptor.java:415) ~[na:1.8.0_72]
    at com.sun.jmx.mbeanserver.JmxMBeanServer.unregisterMBean(JmxMBeanServer.java:546) ~[na:1.8.0_72]
    at com.avaje.ebeaninternal.server.core.DefaultServer.shutdownInternal(DefaultServer.java:406) [avaje-ebeanorm.jar:na]
    at com.avaje.ebeaninternal.server.core.DefaultServer.shutdownManaged(DefaultServer.java:379) [avaje-ebeanorm.jar:na]
[error] c.a.e.s.c.DefaultServer - Error unregistering Ebean Ebean:server=default3
javax.management.InstanceNotFoundException: Ebean:server=default3,key=AutoFetch
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1095) ~[na:1.8.0_72]
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.exclusiveUnregisterMBean(DefaultMBeanServerInterceptor.java:427) ~[na:1.8.0_72]
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.unregisterMBean(DefaultMBeanServerInterceptor.java:415) ~[na:1.8.0_72]
    at com.sun.jmx.mbeanserver.JmxMBeanServer.unregisterMBean(JmxMBeanServer.java:546) ~[na:1.8.0_72]
    at com.avaje.ebeaninternal.server.core.DefaultServer.shutdownInternal(DefaultServer.java:406) [avaje-ebeanorm.jar:na]
    at com.avaje.ebeaninternal.server.core.DefaultServer.shutdownManaged(DefaultServer.java:379) [avaje-ebeanorm.jar:na]

从“[error] c.a.e.s.c.DefaultServer - 错误取消注册Ebean Ebean:s​​erver = default2”到“[error] c.a.e.s.c.DefaultServer - 错误取消注册Ebean Ebean:s​​erver = default38”报告每个错误的第一行。所以我认为这是由于EbeanSevers没有关闭。

那么,我怎样才能正确地关闭它们呢?

1 个答案:

答案 0 :(得分:0)

这通常在存在命名冲突时发生。

你在哪里:

String serverName = "default";
server = Ebean.getServer(serverName);

将其更改为具有唯一服务器名称的内容:

String serverName = "default" + UUID.randomUUID().toString();
server = Ebean.getServer(serverName);

当然你可以使用一些计数器或类似的,只需确保Ebean服务器的名称是唯一的。