Derby服务器启动嵌入在Tomcat 8中

时间:2016-10-23 09:20:32

标签: hibernate derby tomcat8

我想在我的Web应用程序java启动时启动Derby网络服务器.. 所以我尝试用NetworkServerControl实现一个javax.servelet.ServletContextListener:

public class ReqListener implements ServletContextListener {

    static final Logger log =  LoggerFactory.getLogger(ReqListener.class);
    PrintWriter pw = new PrintWriter(System.out);
    private NetworkServerControl derbyserver;

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        try {
            String userHomeDir = System.getProperty("user.home", ".");
            String systemDir = userHomeDir + "/.rubrica";
            // Set the db system directory.
            System.setProperty("derby.system.home", systemDir);

            derbyserver =   new NetworkServerControl(InetAddress.getByName("localhost"), 1527);
            derbyserver.start(pw);
            log.info("Apache derby settings ok");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            log.error(e.getMessage());
        }

    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        if (derbyserver!=null){
            try {
                log.info("Apache derby db server stopping..");
                derbyserver.shutdown();
            } catch (Exception e) {
               log.error(e.getMessage());
                e.printStackTrace();
            }
        }

    }

derby的pom依赖项列表:

  <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <version>10.12.1.1</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derbytools</artifactId>
            <version>10.12.1.1</version>
            <scope>runtime</scope>
        </dependency>

      <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.12.1.1</version>
      </dependency>
        <dependency>
          <groupId>org.apache.derby</groupId>
          <artifactId>derbynet</artifactId>
          <version>10.12.1.1</version>
        </dependency>

但是当Tomcat执行代码时,tomcat会给出以下例外情况:

INFORMAZIONI: Stopping service Catalina
Sun Oct 23 11:02:34 CEST 2016 : Server di rete Apache Derby: 10.12.1.1 - (1704137) avviato e pronto ad accettare connessioni sulla porta 1527
23/10/2016 11:02:34.505 INFO  [localhost-startStop-2] ReqListener: Apache derby settings ok
ott 23, 2016 11:02:34 AM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc
AVVERTENZA: The web application [rubrica] registered the JDBC driver [org.apache.derby.jdbc.AutoloadedDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
ott 23, 2016 11:02:34 AM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
AVVERTENZA: The web application [rubrica] appears to have started a thread named [Timer-0] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 java.lang.Object.wait(Object.java:502)
 java.util.TimerThread.mainLoop(Timer.java:526)
 java.util.TimerThread.run(Timer.java:505)
ott 23, 2016 11:02:34 AM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
AVVERTENZA: The web application [rubrica] appears to have started a thread named [derby.NetworkServerStarter] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.util.zip.Inflater.inflateBytes(Native Method)
 java.util.zip.Inflater.inflate(Inflater.java:259)
 java.util.zip.InflaterInputStream.read(InflaterInputStream.java:152)
 org.apache.catalina.webresources.AbstractArchiveResource$JarInputStreamWrapper.read(AbstractArchiveResource.java:255)
 org.apache.catalina.webresources.AbstractArchiveResource.getContent(AbstractArchiveResource.java:180)
 org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2488)
 org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:859)
 org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1302)
 org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
 org.apache.derby.impl.drda.NetworkServerControlImpl$2.run(Unknown Source)
 org.apache.derby.impl.drda.NetworkServerControlImpl$2.run(Unknown Source)
 java.security.AccessController.doPrivileged(Native Method)
 org.apache.derby.impl.drda.NetworkServerControlImpl.blockingStart(Unknown Source)
 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 java.lang.reflect.Method.invoke(Method.java:498)
 org.apache.derby.iapi.jdbc.DRDAServerStarter.run(Unknown Source)
 java.lang.Thread.run(Thread.java:745)
ott 23, 2016 11:02:34 AM org.apache.coyote.AbstractProtocol stop
INFORMAZIONI: Stopping ProtocolHandler ["http-nio-8080"]

我正在使用JPA和Hibernate,但是服务器Tomcat立即停止,因此我无法查找我的jpa测试页。

会发生什么?怎么了?

谢谢

拉​​吉

0 个答案:

没有答案