Announcement Announcement Module
No announcement yet.
PersistenceContext not injected without @Transactional Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • PersistenceContext not injected without @Transactional

    Hello friends,

    I have a typical service-DAO-Hibernate model application running. Things worked perfectly till a friend questioned my usage of @Transactional annotation for a simple getter method. Leaving aside design philosophies, I tried out a small project where I had no @Transactional annotation on a find method. With my DAO having a @PersistenceContext injection, I would expect Spring to inject in the EntityManager and manage it through the query execution. But apparently I was wrong.

    I have referred to the petclinic example also but dont see anything revealing there.

    Here is my Spring configuration:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
    		xmlns:p="" xmlns:aop=""
    		xmlns:context="" xmlns:jee=""
    	<bean id="jdbc.datasource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true">
    		<property name="driverClassName" value="" />
    		<property name="url" value="jdbc:db2://;"/>
    		<property name="username" value="DB2User" />
    		<property name="password" value="passme" />
          <bean id="entityManagerFactory" 
            <property name="dataSource" ref="jdbc.datasource" />
            <property name="persistenceUnitName" value="sattest" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
                	<property name="showSql" value="true" />
                	<property name="databasePlatform" value="org.hibernate.dialect.DB2Dialect" />
                	<property name="generateDdl" value="false" />
    		<property name="jpaPropertyMap">
    				<entry key="javax.persistence.transactionType" value="RESOURCE_LOCAL" />
    		class="" />
    	<bean id="transactionManager"
    		class="org.springframework.orm.jpa.JpaTransactionManager" >
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
            <property name="dataSource" ref="jdbc.datasource" />
    	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false" />
    	<bean id="basicDao" class="com.sathya.basic.BasicDAO" />
    	<bean id="basicService" class="com.sathya.basic.BasicServiceImpl" >
    		<property name="dao" ref="basicDao" />
    My Service interface:

    package com.sathya.basic;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    public interface IBasicService {
    	@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
    	Book createBook(String name);
    	Book[] findBooksMatchingName(String name);
    and my DAO:

    package com.sathya.basic;
    public class BasicDAO {
       protected EntityManager entityManager;
       public Book createBookBook book){
    	return book;
       public List<Book> findBooksByName(String name) {
    	String queryStr = "select b from " + Book.class.getName() + " b where";
             query = entityManager.createQuery(queryStr);         
            query.setParameter("name", name);
          return (List<Book>) query.getResultList();
    Since the insert happens in a transaction mode, the creation goes through fine. But for a select, as there is no transaction, the below exception is thrown:

    Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.SessionException: Session is closed!
    	at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(
    	at org.hibernate.ejb.QueryImpl.getResultList(
    	at com.sathya.basic.BasicDAO.findBooksByName(
    	at com.sathya.basic.BasicServiceImpl.findBooksMatchingName(
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    	at java.lang.reflect.Method.invoke(
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(
    	at $Proxy13.findBooksMatchingName(Unknown Source)
    	at com.sathya.basic.BasicTestRunner.main(
    Caused by: org.hibernate.SessionException: Session is closed!
    	at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(
    	at org.hibernate.impl.SessionImpl.list(
    	at org.hibernate.impl.QueryImpl.list(
    	at org.hibernate.ejb.QueryImpl.getResultList(
    	... 10 more
    Is a @Transactional attribute then mandatory for the EntityManager to get injected? Is there any configuration I could do so that Spring could create the entityManager on demand and close if at the end of the operation?