Java Web项目+ JPA / JDBC“错误连接到端口1527上的服务器localhost,并显示消息Connection refused”

时间:2012-05-27 21:24:40

标签: java database jpa web persistence

我有这个旧的网络项目,名为“饮料档案”。除其他外,您可以添加,删除,列出和编辑饮料。它从NetBeans本地运行良好,但当我将项目作为.war文件部署到使用Tomcat 6的服务器时,每次我尝试进行数据库操作时都会出现以下错误(例如向db添加饮料):

  

javax.persistence.PersistenceException:Exception [EclipseLink-4002](Eclipse Persistence Services - 2.3.0.v20110604-r9504):org.eclipse.persistence.exceptions.DatabaseException   内部异常:java.sql.SQLNonTransientConnectionException:java.net.ConnectException:在端口1527上连接到服务器localhost时出错,并显示消息Connection refused。   错误代码:40000       org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:501)       org.eclipse.persistence.internal.jpa.EntityManagerFactoryDe​​legate.getDatabaseSession(EntityManagerFactoryDe​​legate.java:188)       org.eclipse.persistence.internal.jpa.EntityManagerFactoryDe​​legate.createEntityManagerImpl(EntityManagerFactoryDe​​legate.java:277)       org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:290)       org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:268)       archive.Register.getEntityManager(Register.java:53)       archive.Register.getDrinks(Register.java:170)       servlets.ListServlet.doGet(ListServlet.java:67)       javax.servlet.http.HttpServlet.service(HttpServlet.java:617)       javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

这是我的源代码,(在NetBeans中我使用GlassFish作为服务器)。

Register.java:

public class Register {

private static Register instance;
private EntityManager em;

private EntityManagerFactory emf = null;

public static Register getInstance() {
    if (instance == null) {
        instance = new Rekisteri();
    }

    return instance;
}

private Register() {
    try {
        Class.forName("org.apache.derby.jdbc.ClientDriver");
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Rekisteri.class.getName()).log(Level.SEVERE, null, ex);
    }
    emf = Persistence.createEntityManagerFactory("DrinksArchivePU");
}

public EntityManager getEntityManager() {
    return emf.createEntityManager();
}

public void addDrink(Drink new) {
    em = getEntityManager();
    em.getTransaction().begin();
    em.merge(new);
    em.getTransaction().commit();     
}

etc...//
}

的persistence.xml:

<?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="DrinksArchivePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
        <property name="eclipselink.jdbc.user" value="d"/> 
        <property name="eclipselink.jdbc.password" value="d"/> 
        <property name="eclipselink.jdbc.url" value="jdbc:derby://localhost:1527/drinks;create=true"/>
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
        <property name="eclipselink.ddl-generation.output-mode" value="database"/>
        <property name="eclipselink.weaving" value="false"/>
        <property name="eclipselink.logging.level" value="FINE"/>
    </properties>
</persistence-unit>

我读到问题可能是因为德比网络服务器没有在我的服务器上启动但是如果是这样,如何解决它?

编辑:已修复,我在persistence.xml设置中更改为HSQLDB JDBC驱动程序,之后就是将不同的jar文件添加到服务器中的项目中。

现在是persistence.xml:

<?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="DrinksAchivePU" transaction-type="RESOURCE_LOCAL">

    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <class>archive.Ingredient</class>
    <class>archive.Review</class>
    <class>archive.Drink</class>
    <class>archive.DrinkCategory</class>
    <class>archive.User</class>
    <class>archive.Register</class>

    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb;default_schema=true"/>
        <property name="javax.persistence.jdbc.user" value="sa"/>
        <property name="javax.persistence.jdbc.password" value=""/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>

1 个答案:

答案 0 :(得分:0)

Glassfish捆绑了Java DB。这就是它适用于本地机器的原因。您的部署服务器具有不同的数据库服务器配置(检查您的persistence.xml)或没有在该端口上运行的数据库服务器。