1
SEVERE: Local Exception Stack: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseExceptionInternal 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.Error Code: 0

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="HelloWorld">
        <class>model.HelloWorld</class>
        <properties>
                <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
                <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/database" />
                <property name="javax.persistence.jdbc.user" value="root" />
                <property name="javax.persistence.jdbc.password" value="password" />
            </properties>
    </persistence-unit>
</persistence>

我在 glassfish 上安装了连接器,通过成功的 ping,我无法让 servlet 和 entitymanager 将查询发送到 mysql 服务器。任何帮助都会很好,还有教程。

4

1 回答 1

1

如果您使用的是 Glassfish 启动的 JDBC 连接(我认为您是因为您说您成功 ping 了数据源),那么您不想像在 persistence.xml 中那样定义属性。您只需按 JNDI 名称指定数据源。

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

  <persistence-unit name="HelloWorld">
    <non-jta-data-source>jdbc/myds</non-jta-data-source>
    <class>model.HelloWorld</class>
    <properties>
    </properties>
  </persistence-unit>

</persistence>
于 2013-07-27T07:48:12.893 回答