I wanted to install @Transactional for my services instead of my DAOs ... in order to open a transaction and be able to use some lazy collections in my services without getting a "LazyInitializationException".
So far, @Transactional been in the DAO, and transactions have been opened and closed when you enter and exit the DAO.
Now that @Transactional in my services, transactions do not even open. Thus, the service calls the DAO without opening a transaction, and we got the following exception: org.hibernate.SessionException: session closed!
We use JPA over Hibernate, here is an excerpt from our configuration:
<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 />
EDIT, thanks for your answers / comments: So, I declare @Transactional on the whole service (implementation, not interface), and not on methods, to be sure that @Transactional will apply to everyone.
@Transactional public class MyServiceImpl implements MyService { public void method1(){...} public void method2(){...} }
I also give you how my DAOs are configured, they all extend the following class:
public abstract class JpaDAOSupport { protected EntityManager em; @PersistenceContext(type = PersistenceContextType.TRANSACTION) public void setEm(EntityManager em) { this.em = em; } }
EDIT2: I simplified my application to have only 1 service and 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; } }
Save configuration file:
<?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>
Note that I do not have access to the "mode" attribute for tx: annotation-driven.
Here are the spring logs in DEBUG startup mode:
[...] 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@7d27 96] 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 [o rg.springframework.ui.context.support.ResourceBundleThemeSource@ 6bbe7] k.beans.factory.support.DefaultListableBeanFactory Pre-instantiating singletons in org.s pringframework.beans.factory.support.DefaultListableBeanFactory@ 1989b5: defining beans [org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor
I would like to understand why transactions do not open and how can I solve this problem?
Thanks,