我正在开发一个使用Spring/Hibernate/Vaadin框架的应用程序,但是尽管做了很多研究,我始终无法解决一个问题。我的应用程序分为四个层次:模型<->存储库<->服务<->视图。
当我在我的笔记本电脑上时,尝试显示我的应用程序的视图时没有任何问题。但是当我将我的应用程序放在预生产服务器上供人们测试时,Tomcat日志会显示以下错误信息:
演员服务
当我在我的笔记本电脑上时,尝试显示我的应用程序的视图时没有任何问题。但是当我将我的应用程序放在预生产服务器上供人们测试时,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);
}
演员服务
/**
* 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();
}