2

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

javax.persistence.PersistenceException: 异常 [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException 内部异常: java.sql.SQLNonTransientConnectionException: java.net.ConnectException:在端口 1527 上连接到服务器 localhost 时出错,并显示连接被拒绝消息。错误代码: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。

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

注册.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...//
}

持久性.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>

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

编辑:已修复,我在我的 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>

4

1 回答 1

0

Glassfish 与 Java DB 捆绑在一起。这就是它在您的本地机器上工作的原因。您的部署服务器要么具有不同的数据库服务器配置(检查您的 persistence.xml),要么在该端口上没有运行数据库服务器。

于 2012-05-28T18:59:26.447 回答