无法将glassfish连接到mysql

时间:2013-07-27 07:12:40

标签: mysql java-ee glassfish

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服务器。任何帮助都会很好,也是教程。

1 个答案:

答案 0 :(得分: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>