我为 glassfish webapp写了以下 persistence.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
version="2.1">
<persistence-unit name="mypu" transaction-type="RESOURCE_LOCAL">
<description>my</description>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>my.OntClass</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/user_lindenb"/>
<property name="javax.persistence.jdbc.user" value="appuser"/>
<property name="javax.persistence.jdbc.password" value="apppassword"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.weaving" value="false"/><!-- http://java.dzone.com/articles/jpa-performance-optimization -->
<property name="eclipselink.target-database" value="MySQL"/>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.logging.parameters" value="false"/>
</properties>
</persistence-unit>
</persistence>
MySQL服务器正在运行,我可以使用:
从servlet连接到MySQL服务器@Resource (name="jdbc/MysqlDS" )
javax.sql.DataSource mysqlDS;
现在,我想使用持久性API。但是,当我想部署我的应用程序时,它会引发以下错误:
remote failure: Error occurred during deployment: Exception while preparing the app : Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused.
据我了解,它试图连接到Derby而不是MySQL?
glassfish log:
(...)
Caused by: org.apache.derby.client.am.DisconnectException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused.
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.initConnection(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.newNetConnection(Unknown Source)
at org.apache.derby.jdbc.ClientDataSource.getConnectionX(Unknown Source)
... 71 more
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at javax.net.DefaultSocketFactory.createSocket(SocketFactory.java:271)
at org.apache.derby.client.net.OpenSocketAction.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 79 more
(...)
我该如何解决这个问题?
感谢。
答案 0 :(得分:2)
替换
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
通过
<jta-data-source>jdbc/MysqlDS</jta-data-source>
现在似乎有效......