配置了derby数据库后启动tomcat 服务报错

在本应用中由于是以derby数据库作为数据存储,采用网络连接方式连接derby,在启动tomcat 服务是进行一些数据库的初始化工作,比如创建表之类的。

所以就特地创建了一个servlet 去做这些初始化工作。

在web.xml中配置如下:

<web-app>

...

<servlet>

        <servlet-name>derbynet</servlet-name>

        <servlet-class>org.apache.derby.drda.NetServlet</servlet-class>

        <init-param>

            <param-name>startNetworkServerOnInit</param-name>

            <param-value>true</param-value>

            <description>Starts the Network Server at servlet

                    initialization if 'true'. </description>

        </init-param>

        <init-param>

            <param-name>portNumber</param-name>

            <param-value>1527</param-value>

            <description>Port number to use. The default is 1527.</description>

        </init-param>

        <init-param>

            <param-name>host</param-name>

            <param-value>localhost</param-value>

            <description> Host Name to use. The default is "localhost". Please secure any server which listens on a public address. </description>

        </init-param>

        <init-param>

            <param-name>tracingDirectory</param-name>

            <param-value />

            <description>Directory where trace files should be placed. </description>

        </init-param>

        <load-on-startup>true</load-on-startup>

    </servlet>

    <servlet>

        <servlet-name>InitializeDatabase</servlet-name>

        <servlet-class>com.sap.pf.prp.servlet.InitializeDatabase</servlet-class>

        <load-on-startup>0</load-on-startup>

    </servlet>

...

</web-app>

一切妥当之后,在eclipse下启动tomcat 服务,一切正常。

但是当我启动tomcat目录下的startup.bat时,就报错了,信息如下:

java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error c

onnecting to server localhost on port 1527 with message Connection refused: conn

ect.

        at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unkn

own Source)

        at org.apache.derby.client.am.SqlException.getSQLException(Unknown Sourc

e)

        at org.apache.derby.jdbc.ClientDriver.connect(Unknown Source)

        at java.sql.DriverManager.getConnection(DriverManager.java:582)

        at java.sql.DriverManager.getConnection(DriverManager.java:185)

        at com.sap.pf.prp.db.DBManager.getConnection(DBManager.java:56)

        at com.sap.pf.prp.db.DBManager.initDB(DBManager.java:27)

        at com.sap.pf.prp.servlet.InitializeDatabase.init(InitializeDatabase.jav

a:13)

        at javax.servlet.GenericServlet.init(GenericServlet.java:212)

        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.

java:1139)

        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:96

6)

        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContex

t.java:3956)

        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4

230)

        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)

        at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)

        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)

        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443

)

        at org.apache.catalina.core.StandardService.start(StandardService.java:4

48)

        at org.apache.catalina.core.StandardServer.start(StandardServer.java:700

)

        at org.apache.catalina.startup.Catalina.start(Catalina.java:552)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.

java:39)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces

sorImpl.java:25)

        at java.lang.reflect.Method.invoke(Method.java:597)

        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)

        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)

Caused by: org.apache.derby.client.am.DisconnectException: java.net.ConnectExcep

tion : Error connecting to server localhost on port 1527 with message Connection

 refused: connect.

        at org.apache.derby.client.net.NetAgent.<init>(Unknown Source)

        at org.apache.derby.client.net.NetConnection.newAgent_(Unknown Source)

        at org.apache.derby.client.am.Connection.<init>(Unknown Source)

        at org.apache.derby.client.net.NetConnection.<init>(Unknown Source)

        at org.apache.derby.client.net.NetConnection40.<init>(Unknown Source)

        at org.apache.derby.client.net.ClientJDBCObjectFactoryImpl40.newNetConne

ction(Unknown Source)

        ... 24 more

Caused by: java.net.ConnectException: Connection refused: connect

        at java.net.PlainSocketImpl.socketConnect(Native Method)

        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)

        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)

        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)

        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)

        at java.net.Socket.connect(Socket.java:529)

        at java.net.Socket.connect(Socket.java:478)

        at java.net.Socket.<init>(Socket.java:375)

        at java.net.Socket.<init>(Socket.java:189)

        at javax.net.DefaultSocketFactory.createSocket(SocketFactory.java:206)

        at org.apache.derby.client.net.OpenSocketAction.run(Unknown Source)

        at java.security.AccessController.doPrivileged(Native Method)

        ... 30 more

Feb 11, 2015 1:34:10 PM org.apache.coyote.http11.Http11BaseProtocol start

INFO: Starting Coyote HTTP/1.1 on http-80

在网上搜了好一阵子,都没有找到真正的解决办法。

https://db.apache.org/derby/docs/10.4/adminguide/tadmincbdjhhfd.html

http://db.apache.org/derby/papers/DerbyTut/ns_intro.html

http://stackoverflow.com/questions/10420902/data-sources-derby-connection-refused

最后经过仔细的排查,发现其实是我自己的web.xml配置中【servlet启动优先级设置】的问题。

只需要将derbynet中:

<load-on-startup>true</load-on-startup> =====> <load-on-startup>0</load-on-startup>

再将初始化数据库的那个servlet的load-on-startup 改为1 即:

<load-on-startup>0</load-on-startup> ====> <load-on-startup>1</load-on-startup>

改完了之后重新发布启动tomcat,一切正常。

相关推荐