我正在开发一个
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();
}
最佳答案 问题出在@PersistenceContext注释中.我有:
@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager entityManager;
我用它代替了:
@PersistenceContext(type = PersistenceContextType.TRANSACTION)
private EntityManager entityManager;