Spring,Hibernate - TransactionException:已经有一个关联的托管连接

时间:2012-03-20 12:58:36

标签: spring hibernate concurrency entitymanager spring-transactions

我正在开发一个Spring / Hibernate / Vaadin应用程序,我遇到了一个问题,即使进行了大量研究,我还是无法解决。 我的应用程序分为四层:model< - >存储库< - >服务< - >图

当我在笔记本电脑上时,当我尝试显示应用程序的视图时,我没有任何问题。但是当我将我的应用程序放在我的预生产服务器上时,人们会尝试访问以进行测试,我在Tomcat的日志中出现以下错误:

Caused by: org.springframework.orm.jpa.JpaSystemException: org.hibernate.TransactionException: Already have an associated managed connection; nested exception is javax.persistence.PersistenceException: org.hibernate.TransactionException: Already have an associated managed connection
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:311)
    at org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:15)
    at com.code.repositories.jpa.SuperRepository.findAll(SuperRepository.java:99)
    at com.code.service.ActorService.findAllAddressTypes(ActorService.java:103)
    at com.code.views.vaadin.views.contact.AddressTypeView.buildLayout(AddressTypeView.java:50)
    at com.code.views.vaadin.layout.objects.CodeView.<init>(CodeView.java:29)
    at com.code.views.vaadin.views.AbstractEntityView.<init>(AbstractEntityView.java:25)
    at com.code.views.vaadin.views.contact.AddressTypeView.<init>(AddressTypeView.java:31)
    ... 65 more
Caused by: javax.persistence.PersistenceException: org.hibernate.TransactionException: Already have an associated managed connection
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1360)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1288)
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1370)
    at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:60)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.enlistInCurrentTransaction(ExtendedEntityManagerCreator.java:421)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.doJoinTransaction(ExtendedEntityManagerCreator.java:398)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:356)
    at $Proxy285.createNamedQuery(Unknown Source)
    ... 71 more
Caused by: org.hibernate.TransactionException: Already have an associated managed connection
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:65)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
    at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1309)
    at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:57)

我认为问题来自各种用户尝试访问相同数据的事实,但我不确定。 你们有没有人遇到过同样的问题吗?

谢谢。

如果您需要更多代码,请不要犹豫。

修改

是的我使用Spring Security,我在我的服务中注入了我的存储库。典型的服务方法是:

AddressTypeView

@Resource private ActorService actorService;

private BeanItemContainer<AddressType> container;
private EntityList list;
private AddressTypeForm form;

/**
 * Build layout
 */
public void buildLayout() {
    super.buildLayout();

    container = new BeanItemContainer<AddressType>(AddressType.class);
    container.addAll(actorService.findAllAddressTypes());

    list = new EntityList();
    list.addActionHandler(new EntityListActionHandler(this));
    list.setContainerDataSource(container);
    list.setVisibleColumns(AddressType.TABLE_FIELDS);
    list.setColumnHeaders(list.getColumnHeaders());

    form = new AddressTypeForm(container);

    list.addListener(new ItemClickEvent.ItemClickListener() {

        private static final long serialVersionUID = 1L;

        @Override
        public void itemClick(ItemClickEvent event) {   
            AddressType selected = (AddressType) event.getItemId();
            form.setItemDataSource(new BeanItem<AddressType>(selected));
            form.setImmediate(true);
        }
    });

    VerticalSplitPanel panel = new VerticalSplitPanel();
    panel.setFirstComponent(list);
    panel.setSecondComponent(form);

    getVerticalLayout().addComponent(toolBar);
    getVerticalLayout().addComponent(panel);
    getVerticalLayout().setExpandRatio(panel, 1);
}

ActorService

/**
 * Find all address types
 * @return
 */
public List<AddressType> findAllAddressTypes() {
    return addressTypeRepository.findAll();
}

AdressTypeRepository

@SuppressWarnings("unchecked")
@Override
@Transactional
public List<T> findAll() {
    Query query = this.entityManager.createNamedQuery(this.entityClass.getSimpleName() + ".findAll");
    return query.getResultList();
}

2 个答案:

答案 0 :(得分:2)

问题出在@PersistenceContext注释中。我有:

@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager entityManager;

我将其替换为:

@PersistenceContext(type = PersistenceContextType.TRANSACTION)
private EntityManager entityManager;

答案 1 :(得分:0)

您是否在笔记本电脑和预生产服务器之间的不同应用服务器上运行?例如。你在笔记本电脑上使用Tomcat,但在pre-prod服务器上使用WebLogic,或类似的东西?如果是这样,差异可能在于DataSource bean的设置