openjpa-2.4.2-r422266:1777108 org.apache.openjpa.persistence.InvalidStateException:事务处于活动状态时无法执行此操作

时间:2019-01-14 19:57:41

标签: openjpa

实体管理器上的关闭操作发生上述异常。我正在尝试启动使用OpenJPA的james服务器。我已经创建了4个实体,并将其添加到james使用的Persistence xml中。我还使用Liquibase为这4个表创建架构。在内存H2数据库中使用时,一切正常,但在James服务器中运行时,一切都会出错。

我一直在努力解决这个问题,但找不到原因,因此无法找到解决方法。

我尝试了以下操作,但除此之外,我想不出任何可能相关的内容:

  1. 评论了persistence.xml的同步映射部分,因为我不希望JPA创建尚不存在的表。

有人知道这可能是根本原因吗?感谢您的回应。

这是我的相关代码:

persistence.xml

    <persistence 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"
version="2.0">

<persistence-unit name="James" transaction-type="RESOURCE_LOCAL">
    <!-- Mailbox stuff-->
    <class>org.apache.james.mailbox.jpa.mail.model.JPAMailbox</class>
    <class>org.apache.james.mailbox.jpa.mail.model.JPAUserFlag</class>
   <class>org.apache.james.mailbox.jpa.mail.model.openjpa
.AbstractJPAMailboxMessage</class>

 <class>org.apache.james.mailbox.jpa.mail.
 model.openjpa.JPAMailboxMessage</class>
    <class>org.apache.james.mailbox.jpa.mail.model.JPAProperty</class>
    <class>org.apache.james.mailbox.jpa.mail.
 model.JPAMailboxAnnotation</class>
    <class>org.apache.james.mailbox.jpa.user.model.JPASubscription</class>
    <class>org.apache.james.domainlist.jpa.model.JPADomain</class>
    <class>org.apache.james.mailrepository.jpa.JPAUrl</class>
    <class>org.apache.james.user.jpa.model.JPAUser</class>
    <class>org.apache.james.rrt.jpa.model.JPARecipientRewrite</class>
    <class>org.apache.james.mailbox.jpa.quota.model.
 MaxDomainMessageCount</class>
    <class>org.apache.james.mailbox.jpa.quota.model.
 MaxDomainStorage</class>
    <class>org.apache.james.mailbox.jpa.quota.model.
 MaxGlobalMessageCount</class>
    <class>org.apache.james.mailbox.jpa.quota.model.
 MaxGlobalStorage</class>
    <class>org.apache.james.mailbox.jpa.quota.model.
 MaxUserMessageCount</class>
    <class>org.apache.james.mailbox.jpa.quota.model.
 MaxUserStorage</class>
    <class>org.apache.james.mailbox.jpa.quota.model.
 MaxDomainStorage</class>
    <class>org.apache.james.mailbox.jpa.quota.model. 
 MaxDomainMessageCount</class>
    <class>org.apache.james.mailbox.jpa.quota.model.  
 JpaCurrentQuota</class>

    <class>com.xxxx.xxxx.model.xxxUser</class>
    <class>com.xxxx.xxxx.model.BlockedEmail</class>
    <class>com.xxxx.xxxx.model.xxxx</class>
    <class>com.xxxx.xxxx.model.xxxxEmail</class>

    <properties>
        <!-- <property name="openjpa.jdbc.SynchronizeMappings" 
    value="buildSchema(ForeignKeys=true)"/> -->
        <property name="openjpa.jdbc.MappingDefaults" 
 value="ForeignKeyDeleteAction=cascade, 
  JoinForeignKeyDeleteAction=cascade"/>
        <property name="openjpa.jdbc.SchemaFactory" 
 value="native(ForeignKeys=true)"/>
        <property name="openjpa.jdbc.QuerySQLCache" value="false"/>
    </properties>

</persistence-unit>

</persistence>

我有相应的模型/存储库和服务类。

存储库示例:

import java.util.Locale;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceException;
import javax.persistence.PersistenceUnit;
import org.apache.james.user.api.UsersRepositoryException;
import org.apache.james.user.api.model.User;  
import org.apache.james.user.jpa.model.JPAUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class JPAUsersRepository {
private static final Logger LOGGER = 
LoggerFactory.getLogger(JPAUsersRepository.class);
private EntityManagerFactory entityManagerFactory;
private String algo;
/**
 * Sets entity manager.
 * 
 * @param entityManagerFactory
 *            the entityManager to set
 */
@Autowired
@PersistenceUnit(unitName = "James")
public final void setEntityManagerFactory(EntityManagerFactory 
 entityManagerFactory) {
    this.entityManagerFactory = entityManagerFactory;
}
@PostConstruct
public void init() {
    createEntityManager().close();
}
/**
 * Get the user object with the specified user name. Return null if no such
 * user.
 * 
 * @param name
 *            the name of the user to retrieve
 * @return the user being retrieved, null if the user doesn't exist
 * 
 * @since James 1.2.2
 */
public User getUserByName(String name) throws UsersRepositoryException {
    EntityManager entityManager = 
entityManagerFactory.createEntityManager();
    try {
        return (JPAUser) 
 entityManager.createNamedQuery("findUserByName").setParameter("name", 
 name).getSingleResult();
    } catch (NoResultException e) {
        return null;
    } catch (PersistenceException e) {
        LOGGER.debug("Failed to find user", e);
        throw new UsersRepositoryException("Unable to search user", e);
    } finally {
        entityManager.close();
    }
}
/**
 * Returns whether or not this user is in the repository
 * 
 * @param name
 *            the name to check in the repository
 * @return whether the user is in the repository
 * @throws UsersRepositoryException
 */
public boolean contains(String name) throws UsersRepositoryException {
    EntityManager entityManager = 
entityManagerFactory.createEntityManager();
    try {
        return (Long) entityManager.createNamedQuery("containsUser")
            .setParameter("name", name.toLowerCase(Locale.US))
            .getSingleResult() > 0;
    } catch (PersistenceException e) {
        LOGGER.debug("Failed to find user", e);
        throw new UsersRepositoryException("Failed to find user" + name, e);
    } finally {
        entityManager.close();
    }
}
/**
 * Return a new {@link EntityManager} instance
 * 
 * @return manager
 */
private EntityManager createEntityManager() {
    return entityManagerFactory.createEntityManager();
}
}

非常感谢, 榕树蝙蝠

0 个答案:

没有答案
相关问题