gpt4 book ai didi

Spring、Hibernate - TransactionException : Already have an associated managed connection

转载 作者:行者123 更新时间:2023-12-02 01:25:13 26 4
gpt4 key购买 nike

我正在开发一个 Spring/Hibernate/Vaadin 应用程序,尽管进行了大量研究,但我仍然无法解决一个问题。我的应用程序分为四层:模型 <-> 存储库 <-> 服务 <-> View

当我使用笔记本电脑时,尝试显示应用程序的 View 时没有任何问题。但是,当我将应用程序放在预生产服务器上时,人们尝试访问进行测试,并且 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,我将存储库注入(inject)到我的服务中。典型的服务方法是:

地址类型 View

@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);
}

Actor 服务

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

地址类型存储库

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

最佳答案

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

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

我将其替换为:

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

关于Spring、Hibernate - TransactionException : Already have an associated managed connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9787217/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com