Не удалось открыть весеннюю транзакцию на сервисе - PullRequest
4 голосов
/ 06 января 2011

Я хотел установить @Transactional на своих сервисах вместо моих DAO ... чтобы открыть транзакцию и иметь возможность использовать некоторые ленивые коллекции в моих сервисах без получения "LazyInitializationException".

До сих пор @Transactional были в DAO, и транзакции открывались и закрывались при входе в DAO и выходе из них.

Теперь, с @Transactional на моих сервисах, транзакции даже не открываются. Поэтому служба вызывает DAO без открытой транзакции, и мы получили следующее исключение: org.hibernate.SessionException: Сессия закрыта!

Мы используем JPA поверх Hibernate, вот выдержка из нашей конфигурации:

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"/>  

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven />

РЕДАКТИРОВАТЬ, спасибо за ваши ответы / комментарии: Итак, я объявляю @Transactional на весь сервис (реализацию, а не интерфейс), а не на методы, чтобы быть уверенным, что @Transactional будет применяться ко всем.

@Transactional
public class MyServiceImpl implements MyService {
  public void method1(){...}
  public void method2(){...}
}

Я также даю вам способ настройки моих DAO, все они расширяют следующий класс:

public abstract class JpaDAOSupport {
  protected EntityManager em;
  @PersistenceContext(type = PersistenceContextType.TRANSACTION)
  public void setEm(EntityManager em) {
    this.em = em;
  }
}

EDIT2: Я упростила свое приложение, чтобы иметь только 1 службу и 1 DAO.

MyService:

@Transactional
public class MyServiceImpl implements MyService {

  private MyDAO myDAO;

  public void setMyDAO(MyDAO myDAO) {
    this.myDAO = myDAO;
  }


  @Override
  public void method1() {
    List<String> things = myDAO.getAll();
    System.out.println("Test size: " + things.size());
  }

  @Override
  public void method2() {
    List<String> things = myDAO.getAll();
    for (String thing : things) {
      System.out.println(thing);
    }
  }
}

MyDAO

@Repository
public class MyDAOImpl extends JpaDAOSupport implements MyDAO {

  @SuppressWarnings("unchecked")
  @Override
  public List<String> getAll() {
    Query query = em.createQuery("FROM " + Cfg.class.getName());
    List<Cfg> result = query.getResultList();
    List<String> realResult = new ArrayList<String>();
    for (Cfg cfg : result) {
      realResult.add(cfg.getKey());
    }
    return realResult;
  }
}

Файл конфигурации постоянства:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">


  <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

  <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"/>    

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

  <bean id="my.package.dao.MyDAO" class="my.package.dao.jpa.MyDAOImpl" />

  <bean id="my.package.service.MyService" class="my.package.service.impl.MyServiceImpl" init-method="method1">
    <property name="myDAO" ref="my.package.dao.MyDAO" />
  </bean>
</beans>

Обратите внимание, что у меня нет доступа к атрибуту "mode" для tx: annotation-driven.

Вот журналы пружины в режиме DEBUG при запуске:

[...]
k.beans.factory.support.DefaultListableBeanFactory  Creating shared instance of singleton bean 'entityManagerFactory'
k.beans.factory.support.DefaultListableBeanFactory  Creating instance of bean 'entityManagerFactory'
k.beans.factory.support.DefaultListableBeanFactory  Eagerly caching bean 'entityManagerFactory' to allow for resolving potential circular references
g.springframework.beans.CachedIntrospectionResults  Getting BeanInfo for class [org.springframework.orm.jpa.LocalEntityManagerFactoryBean]
g.springframework.beans.CachedIntrospectionResults  Caching PropertyDescriptors for class [org.springframework.orm.jpa.LocalEntityManagerFactoryBean]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'beanClassLoader' of type [java.lang.ClassLoader]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'class' of type [java.lang.Class]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'dataSource' of type [javax.sql.DataSource]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'entityManagerFactoryInterface' of type [java.lang.Class]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'entityManagerInterface' of type [java.lang.Class]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'jpaDialect' of type [org.springframework.orm.jpa.JpaDialect]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'jpaProperties' of type [java.util.Properties]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'jpaPropertyMap' of type [java.util.Map]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'jpaVendorAdapter' of type [org.springframework.orm.jpa.JpaVendorAdapter]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'nativeEntityManagerFactory' of type [javax.persistence.EntityManagerFactory]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'object' of type [javax.persistence.EntityManagerFactory]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'objectType' of type [java.lang.Class]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'persistenceProvider' of type [javax.persistence.spi.PersistenceProvider]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'persistenceProviderClass' of type [java.lang.Class]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'persistenceUnitInfo' of type [javax.persistence.spi.PersistenceUnitInfo]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'persistenceUnitName' of type [java.lang.String]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'singleton' of type [boolean]
k.beans.factory.support.DefaultListableBeanFactory  Invoking afterPropertiesSet() on bean with name 'entityManagerFactory'
ingframework.orm.jpa.LocalEntityManagerFactoryBean  Building JPA EntityManagerFactory for persistence unit 'null'
ework.web.context.support.XmlWebApplicationContext  Bean 'entityManagerFactory' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
k.beans.factory.support.DefaultListableBeanFactory  Finished creating instance of bean 'entityManagerFactory'
k.beans.factory.support.DefaultListableBeanFactory  Finished creating instance of bean 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0'
k.beans.factory.support.DefaultListableBeanFactory  Creating shared instance of singleton bean 'org.springframework.aop.config.internalAutoProxyCreator'
k.beans.factory.support.DefaultListableBeanFactory  Creating instance of bean 'org.springframework.aop.config.internalAutoProxyCreator'
org.springframework.core.CollectionFactory          Creating [java.util.concurrent.ConcurrentHashMap]
k.beans.factory.support.DefaultListableBeanFactory  Eagerly caching bean 'org.springframework.aop.config.internalAutoProxyCreator' to allow for resolving potential circular references
g.springframework.beans.CachedIntrospectionResults  Getting BeanInfo for class [org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator]
g.springframework.beans.CachedIntrospectionResults  Caching PropertyDescriptors for class [org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'advisorAdapterRegistry' of type [org.springframework.aop.framework.adapter.AdvisorAdapterRegistry]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'applyCommonInterceptorsFirst' of type [boolean]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'beanClassLoader' of type [java.lang.ClassLoader]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'beanFactory' of type [org.springframework.beans.factory.BeanFactory]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'class' of type [java.lang.Class]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'customTargetSourceCreators' of type [[Lorg.springframework.aop.framework.autoproxy.TargetSourceCreator;]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'exposeProxy' of type [boolean]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'frozen' of type [boolean]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'interceptorNames' of type [[Ljava.lang.String;]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'opaque' of type [boolean]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'optimize' of type [boolean]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'order' of type [int]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'proxyClassLoader' of type [java.lang.ClassLoader]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'proxyTargetClass' of type [boolean]
k.beans.factory.support.DefaultListableBeanFactory  Finished creating instance of bean 'org.springframework.aop.config.internalAutoProxyCreator'
ework.web.context.support.XmlWebApplicationContext  Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@7d2796]
ework.web.context.support.XmlWebApplicationContext  Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@1335207]
ework.ui.context.support.UiApplicationContextUtils  Unable to locate ThemeSource with name 'themeSource': using default [org.springframework.ui.context.support.ResourceBundleThemeSource@6bbe7]
k.beans.factory.support.DefaultListableBeanFactory  Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1989b5: defining beans [org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,entityManagerFactory,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,my.package.dao.MyDAO,my.package.service.MyService]; root of factory hierarchy
k.beans.factory.support.DefaultListableBeanFactory  Returning cached instance of singleton bean 'org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0'
k.beans.factory.support.DefaultListableBeanFactory  Returning cached instance of singleton bean 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0'
k.beans.factory.support.DefaultListableBeanFactory  Returning cached instance of singleton bean 'entityManagerFactory'
k.beans.factory.support.DefaultListableBeanFactory  Creating shared instance of singleton bean 'transactionManager'
k.beans.factory.support.DefaultListableBeanFactory  Creating instance of bean 'transactionManager'
k.beans.factory.support.DefaultListableBeanFactory  Eagerly caching bean 'transactionManager' to allow for resolving potential circular references
g.springframework.beans.CachedIntrospectionResults  Getting BeanInfo for class [org.springframework.orm.jpa.JpaTransactionManager]
g.springframework.beans.CachedIntrospectionResults  Caching PropertyDescriptors for class [org.springframework.orm.jpa.JpaTransactionManager]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'class' of type [java.lang.Class]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'dataSource' of type [javax.sql.DataSource]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'defaultTimeout' of type [int]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'entityManagerFactory' of type [javax.persistence.EntityManagerFactory]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'failEarlyOnGlobalRollbackOnly' of type [boolean]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'globalRollbackOnParticipationFailure' of type [boolean]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'jpaDialect' of type [org.springframework.orm.jpa.JpaDialect]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'jpaProperties' of type [java.util.Properties]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'jpaPropertyMap' of type [java.util.Map]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'nestedTransactionAllowed' of type [boolean]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'resourceFactory' of type [java.lang.Object]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'rollbackOnCommitFailure' of type [boolean]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'transactionSynchronization' of type [int]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'transactionSynchronizationName' of type [java.lang.String]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'validateExistingTransaction' of type [boolean]
k.beans.factory.support.DefaultListableBeanFactory  Returning cached instance of singleton bean 'entityManagerFactory'
k.beans.factory.support.DefaultListableBeanFactory  Creating shared instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
k.beans.factory.support.DefaultListableBeanFactory  Creating instance of bean 'org.springframework.transaction.config.internalTransactionAdvisor'
rk.autoproxy.InfrastructureAdvisorAutoProxyCreator  Did not attempt to auto-proxy infrastructure class [org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor]
k.beans.factory.support.DefaultListableBeanFactory  Eagerly caching bean 'org.springframework.transaction.config.internalTransactionAdvisor' to allow for resolving potential circular references
g.springframework.beans.CachedIntrospectionResults  Getting BeanInfo for class [org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor]
g.springframework.beans.CachedIntrospectionResults  Caching PropertyDescriptors for class [org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'advice' of type [org.aopalliance.aop.Advice]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'adviceBeanName' of type [java.lang.String]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'beanFactory' of type [org.springframework.beans.factory.BeanFactory]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'class' of type [java.lang.Class]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'classFilter' of type [org.springframework.aop.ClassFilter]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'order' of type [int]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'perInstance' of type [boolean]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'pointcut' of type [org.springframework.aop.Pointcut]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'transactionAttributeSource' of type [org.springframework.transaction.interceptor.TransactionAttributeSource]
k.beans.factory.support.DefaultListableBeanFactory  Creating shared instance of singleton bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
k.beans.factory.support.DefaultListableBeanFactory  Creating instance of bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
org.springframework.core.CollectionFactory          Creating [java.util.concurrent.ConcurrentHashMap]
k.beans.factory.support.DefaultListableBeanFactory  Eagerly caching bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0' to allow for resolving potential circular references
g.springframework.beans.CachedIntrospectionResults  Getting BeanInfo for class [org.springframework.transaction.annotation.AnnotationTransactionAttributeSource]
g.springframework.beans.CachedIntrospectionResults  Caching PropertyDescriptors for class [org.springframework.transaction.annotation.AnnotationTransactionAttributeSource]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'class' of type [java.lang.Class]
k.beans.factory.support.DefaultListableBeanFactory  Finished creating instance of bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
k.beans.factory.support.DefaultListableBeanFactory  Finished creating instance of bean 'org.springframework.transaction.config.internalTransactionAdvisor'
k.beans.factory.support.DefaultListableBeanFactory  Invoking afterPropertiesSet() on bean with name 'transactionManager'
k.beans.factory.support.DefaultListableBeanFactory  Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
k.beans.factory.support.DefaultListableBeanFactory  Finished creating instance of bean 'transactionManager'
k.beans.factory.support.DefaultListableBeanFactory  Returning cached instance of singleton bean 'org.springframework.aop.config.internalAutoProxyCreator'
k.beans.factory.support.DefaultListableBeanFactory  Returning cached instance of singleton bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
k.beans.factory.support.DefaultListableBeanFactory  Creating shared instance of singleton bean 'org.springframework.transaction.interceptor.TransactionInterceptor#0'
k.beans.factory.support.DefaultListableBeanFactory  Creating instance of bean 'org.springframework.transaction.interceptor.TransactionInterceptor#0'
rk.autoproxy.InfrastructureAdvisorAutoProxyCreator  Did not attempt to auto-proxy infrastructure class [org.springframework.transaction.interceptor.TransactionInterceptor]
k.beans.factory.support.DefaultListableBeanFactory  Eagerly caching bean 'org.springframework.transaction.interceptor.TransactionInterceptor#0' to allow for resolving potential circular references
g.springframework.beans.CachedIntrospectionResults  Getting BeanInfo for class [org.springframework.transaction.interceptor.TransactionInterceptor]
g.springframework.beans.CachedIntrospectionResults  Caching PropertyDescriptors for class [org.springframework.transaction.interceptor.TransactionInterceptor]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'class' of type [java.lang.Class]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'transactionAttributeSource' of type [org.springframework.transaction.interceptor.TransactionAttributeSource]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'transactionAttributeSources' of type [[Lorg.springframework.transaction.interceptor.TransactionAttributeSource;]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'transactionAttributes' of type [java.util.Properties]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'transactionManager' of type [org.springframework.transaction.PlatformTransactionManager]
k.beans.factory.support.DefaultListableBeanFactory  Returning cached instance of singleton bean 'transactionManager'
k.beans.factory.support.DefaultListableBeanFactory  Returning cached instance of singleton bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
k.beans.factory.support.DefaultListableBeanFactory  Invoking afterPropertiesSet() on bean with name 'org.springframework.transaction.interceptor.TransactionInterceptor#0'
k.beans.factory.support.DefaultListableBeanFactory  Finished creating instance of bean 'org.springframework.transaction.interceptor.TransactionInterceptor#0'
k.beans.factory.support.DefaultListableBeanFactory  Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
k.beans.factory.support.DefaultListableBeanFactory  Creating shared instance of singleton bean 'my.package.dao.MyDAO'
k.beans.factory.support.DefaultListableBeanFactory  Creating instance of bean 'my.package.dao.MyDAO'
g.springframework.beans.CachedIntrospectionResults  Getting BeanInfo for class [my.package.dao.jpa.JpaDAOSupport]
g.springframework.beans.CachedIntrospectionResults  Caching PropertyDescriptors for class [my.package.dao.jpa.JpaDAOSupport]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'class' of type [java.lang.Class]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'em' of type [javax.persistence.EntityManager]
amework.beans.factory.annotation.InjectionMetadata  Found injected method on class [my.package.dao.jpa.MyDAOImpl]: PersistenceElement for public void my.package.dao.jpa.JpaDAOSupport.setEm(javax.persistence.EntityManager)
k.beans.factory.support.DefaultListableBeanFactory  Eagerly caching bean 'my.package.dao.MyDAO' to allow for resolving potential circular references
g.springframework.beans.CachedIntrospectionResults  Getting BeanInfo for class [my.package.dao.jpa.MyDAOImpl]
g.springframework.beans.CachedIntrospectionResults  Caching PropertyDescriptors for class [my.package.dao.jpa.MyDAOImpl]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'all' of type [java.util.List]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'class' of type [java.lang.Class]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'em' of type [javax.persistence.EntityManager]
amework.beans.factory.annotation.InjectionMetadata  Processing injected method of bean 'my.package.dao.MyDAO': PersistenceElement for public void my.package.dao.jpa.JpaDAOSupport.setEm(javax.persistence.EntityManager)
k.beans.factory.support.DefaultListableBeanFactory  Returning cached instance of singleton bean 'entityManagerFactory'
k.beans.factory.support.DefaultListableBeanFactory  Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
org.springframework.core.CollectionFactory          Creating [java.util.concurrent.ConcurrentHashMap]
g.springframework.aop.framework.JdkDynamicAopProxy  Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [my.package.dao.jpa.MyDAOImpl@1298826]
k.beans.factory.support.DefaultListableBeanFactory  Finished creating instance of bean 'my.package.dao.MyDAO'
k.beans.factory.support.DefaultListableBeanFactory  Creating shared instance of singleton bean 'my.package.service.MyService'
k.beans.factory.support.DefaultListableBeanFactory  Creating instance of bean 'my.package.service.MyService'
k.beans.factory.support.DefaultListableBeanFactory  Eagerly caching bean 'my.package.service.MyService' to allow for resolving potential circular references
g.springframework.beans.CachedIntrospectionResults  Getting BeanInfo for class [my.package.service.impl.MyServiceImpl]
g.springframework.beans.CachedIntrospectionResults  Caching PropertyDescriptors for class [my.package.service.impl.MyServiceImpl]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'class' of type [java.lang.Class]
g.springframework.beans.CachedIntrospectionResults  Found bean property 'myDAO' of type [my.package.dao.MyDAO]
k.beans.factory.support.DefaultListableBeanFactory  Returning cached instance of singleton bean 'my.package.dao.MyDAO'
k.beans.factory.support.DefaultListableBeanFactory  Invoking init method  'method1' on bean with name 'my.package.service.MyService'
anagerCreator$SharedEntityManagerInvocationHandler  Creating new EntityManager for shared EntityManager invocation
.springframework.orm.jpa.EntityManagerFactoryUtils  Closing JPA EntityManager
k.beans.factory.support.DefaultListableBeanFactory  Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1989b5: defining beans [org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,entityManagerFactory,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,my.package.dao.MyDAO,my.package.service.MyService]; root of factory hierarchy
k.beans.factory.support.DefaultListableBeanFactory  Retrieved dependent beans for bean 'my.package.dao.MyDAO': [my.package.service.MyService]
k.beans.factory.support.DefaultListableBeanFactory  Retrieved dependent beans for bean 'transactionManager': [org.springframework.transaction.interceptor.TransactionInterceptor#0]
k.beans.factory.support.DefaultListableBeanFactory  Retrieved dependent beans for bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0': [org.springframework.transaction.config.internalTransactionAdvisor, org.springframework.transaction.interceptor.TransactionInterceptor#0]
k.beans.factory.support.DefaultListableBeanFactory  Retrieved dependent beans for bean 'entityManagerFactory': [transactionManager, my.package.dao.MyDAO]
mework.beans.factory.support.DisposableBeanAdapter  Invoking destroy() on bean with name 'entityManagerFactory'
ingframework.orm.jpa.LocalEntityManagerFactoryBean  Closing JPA EntityManagerFactory for persistence unit 'null'
org.springframework.web.context.ContextLoader       Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'my.package.service.MyService' defined in class path resource [config/persistenceContext.xml]: Invocation of init method failed; nested exception is org.springframework.orm.jpa.JpaSystemException: org.hibernate.SessionException: Session is closed!; nested exception is javax.persistence.PersistenceException: org.hibernate.SessionException: Session is closed!

Я бы хотел понять, почему транзакции не открываются и как я могу решить эту проблему?

Спасибо

Ответы [ 4 ]

6 голосов
/ 13 января 2011

Хорошо, на самом деле проблемы были

  • проблема конфигурации
  • транзакции не могут быть открыты в методе init, потому что (транзакционный) прокси еще не создан.

Для 1.:

    <!-- transaction manager for use with a single JPA EntityManagerFactory for transactional data access to a single datasource -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
    p:entityManagerFactory-ref="entityManagerFactory" 
/>

<!-- uses the persistence unit defined in the META-INF/persistence.xml JPA configuration file -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource">
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        </props>
    </property>
</bean>


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${dataSource.driverClassName}"
    p:url="${dataSource.url}"
    p:username="${dataSource.username}"
    p:password="${dataSource.password}"
/>

около 2.:

  • с использованием шаблона транзакции
  • зарегистрируйте ApplicationListener и прослушайте ContextRefreshedEvent
6 голосов
/ 06 января 2011

Звучит так, как будто это может быть один из:

  1. Ваша аннотация @Transactional находится на интерфейсе, и вы используете неправильный тип прокси

  2. Ваш внутренний метод @Transactional

@Transactional документы

Spring рекомендует аннотировать только конкретные классы (и методы конкретных классов) аннотацией @Transactional, а не аннотировать интерфейсы. Вы, конечно, можете разместить аннотацию @Transactional на интерфейсе (или методе интерфейса), но это работает только так, как вы ожидаете, если вы используете прокси на основе интерфейса. Тот факт, что аннотации Java не наследуются от интерфейсов, означает, что если вы используете прокси на основе классов (proxy-target-class="true") или аспект на основе ткачества (mode="aspectj"), то параметры транзакции не распознаются инфраструктурой прокси и ткачества и объект не будет помещен в транзакционный прокси-сервер, что было бы явно плохо.

Примечание В режиме прокси (который используется по умолчанию) перехватываются только внешние вызовы методов, поступающие через прокси. Это означает, что самовывоз, по сути, метод в целевом объекте, вызывающий другой метод целевого объекта, не приведет к реальной транзакции во время выполнения, даже если вызванный метод помечен @Transactional.

EDIT: Вы также должны проверить документы для LocalEntityManagerFactoryBean, который вы используете. Судя по моему быстрому прочтению, кажется, вам не хватает свойства, которое может понадобиться. Здесь также упоминаются некоторые особенности ткачества, которые могут сыграть здесь свою роль.

Spring ORM JPA LocalEntityManagerFactoryBean docs

2 голосов
/ 06 января 2011

Похоже на старую проблему с прокси.- Используйте реальное ткачество AspectJ вместо этого Spring Proxy, чтобы избавиться от проблемы (навсегда).

@ see: Spring: Диспетчер транзакций на основе аннотаций

0 голосов
/ 09 декабря 2013

Помимо вышеуказанных причин, это также может быть проблемой с версией JRE.На самом деле я столкнулся с тем же.Приложение работало правильно в JRE 6, но когда я перешел на JRE 7, оно вызвало это исключение.

Решение: Указано, что Tomcat явно использует JRE 6 (с использованием Eclipse) вместо JRE 7

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...