在apache-tomee中使用H2的JPA问题

时间:2015-07-07 09:52:00

标签: h2 openjpa tomee

我正在尝试使用apache-tomee-plus-1.6.0.2中的示例JPA应用程序。正在使用的数据库是H2。

当我尝试重新部署webapp时,tomee日志中有Database may be already in use: Locked by another process.个例外。实际上,当重新部署webapp时,我看到锁被保持并且没有被释放。

请查找相关文件的内容。

context.xml中

<Resource
        name="jdbc/ContactsDB"
        auth="Container"
        type="javax.sql.DataSource"
        driverClassName="org.h2.Driver"
        url="jdbc:h2:./repository/deployment/server/webapps/jpa-contacts-database/contacts;"
        username="sa"
        password="sa"
        JtaManaged="true"/>

的persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="ContactsUnit">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <class>org.wso2.appserver.sample.ee.cdi.jpa.jaxws.Contact</class>
    <properties>
        <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
        <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"></property>
    </properties>
</persistence-unit>

ContactManagerImpl.java

package org.wso2.appserver.sample.ee.cdi.jpa.jaxws;

import java.util.List;
import javax.ejb.Stateful;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@Stateful
@Named
public class ContactManagerImpl implements ContactManager {
private static final Log log = LogFactory.getLog(ContactManagerImpl.class);

@PersistenceContext(name = "ContactsUnit", type = PersistenceContextType.EXTENDED)
EntityManager entityManager;

@Override
public String addContact(Contact contact) throws Exception {
    try {
        entityManager.persist(contact);
        return "Contact was saved successfully.";
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return "Error Occurred : " + e.getMessage();
    }
}

@Override
public List<Contact> getContacts() {
    Query query = entityManager.createQuery("SELECT Contact contact FROM Contact contact");
    return query.getResultList();
}

}

我如何克服这个问题?我在这里做了什么错误的代码/配置吗?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。在webapps目录之外提供URL解决了这个问题。例如,我选择〜/ h2webdb / webui