Announcement Announcement Module
Collapse
No announcement yet.
unable to insert/update persist/merge using spring DAO - No JTA transaction found Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • unable to insert/update persist/merge using spring DAO - No JTA transaction found

    My App is built on Spring 3.0.5 and deployed in JBoss 5.1.
    My DAOs are working fine for any get or getAll operations on any of the tables but any inserts or updates does nothing. I do not see any exceptions in the JBoss logs either but I do see the below line when I enable debug on JBoss. Can you please tell me what I am missing?
    I suppose my DAO is unable to create or reference a transaction for the save(object) operation which I have annotated as REQUIRES_NEW.


    Code:
    17:10:01,505 DEBUG [AbstractEntityManagerImpl] Looking for a JTA transaction to join
    17:10:01,520 DEBUG [AbstractEntityManagerImpl] No JTA transaction found
    Here's my spring-jpa.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:p="http://www.springframework.org/schema/p" xmlns:jee="http://www.springframework.org/schema/jee"
    	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context-3.0.xsd
                http://www.springframework.org/schema/jee
                http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
                http://www.springframework.org/schema/tx 
                http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      			http://www.springframework.org/schema/aop
    		    http://www.springframework.org/schema/aop/spring-aop.xsd">
    
                
    
    
    	<!-- JPAAccountDAO has JPA annotations to access EntityManager -->
        <context:annotation-config/>
    
    	
    	<jee:jndi-lookup id="entityManagerFactory" jndi-name="java:/ties-2EntityManagerFactory">
    	</jee:jndi-lookup>
    	
    	<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
              <property name="transactionManagerName" value="java:/TransactionManager"/>
            <property name="userTransactionName" value="UserTransaction"/>
        </bean>
    
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    
    	<!-- Database LOB Handling -->
    	<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" />
    
    	<!-- Read in DAOs from the JPA package -->
    	<context:component-scan base-package="com.rtd.ties2.dao" />	
    </beans>
    My persistence.xml
    Code:
       <persistence-unit name="ties-2" transaction-type="JTA">
          <provider>org.hibernate.ejb.HibernatePersistence</provider>
          <jta-data-source>java:/ties-2Datasource</jta-data-source>
          <!-- The <jar-file> element is necessary if you put the persistence.xml in the WAR and the classes in the JAR -->
    
          <properties>
             <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
    <!--         <property name="hibernate.hbm2ddl.auto" value="validate"/>-->
    <!--		 <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>-->
             <property name="hibernate.show_sql" value="true"/>
             <property name="hibernate.format_sql" value="true"/>
             <property name="jboss.entity.manager.factory.jndi.name" value="java:/ties-2EntityManagerFactory"/>
               
          </properties>
       </persistence-unit>
        
    </persistence>
    My DAO code

    Code:
    package com.rtd.ties2.dao;
    
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import com.rtddenver.gwt.client.entity.TiesVoteScheduleEntries;
    
    @Repository("tiesVoteScheduleEntriesDaoJpa")
    public class TiesVoteScheduleEntriesDaoJpa extends GenericDaoJpa<TiesVoteScheduleEntries>  implements TiesVoteScheduleEntriesDao {
    	
    private static final Log log = LogFactory.getLog(TiesVotesDaoJpa.class);
    	
    	public TiesVoteScheduleEntriesDaoJpa(){
    		super(TiesVoteScheduleEntries.class);
    	}
    	
    	@PersistenceContext
    	protected EntityManager entityManager;
    	public void setEntityManager(EntityManager entityManager){
    		this.entityManager = entityManager;
    	}
    	
    	@Transactional(propagation = Propagation.REQUIRES_NEW)
    	public void save(TiesVoteScheduleEntries object) {
    		log.debug("persisting TiesVoteScheduleEntries instance");
    		try {			
    			 entityManager.persist(object);
    			log.info("persist successful");
    		} catch (RuntimeException re) {
    			log.error("persist failed", re);
    			throw re;
    		}
    		catch (Exception e) {
    			log.error("persist failed", e);
    			e.printStackTrace();
    		}	
    	}
    }
    Code:
    public class GenericDaoJpa<T extends DomainObject> implements GenericDao<T> {
    	
    	
    	private Class<T> type;
    	private static final Log log = LogFactory.getLog("GenericDaoJpa");
    	
    	@PersistenceContext
    	protected EntityManager entityManager;
    	public void setEntityManager(EntityManager entityManager){
    		this.entityManager = entityManager;
    	}
    	
    	public GenericDaoJpa(Class<T> type){
    		super();
    		this.type = type;
    	}
    
    	@Override
    	//@Transactional(readOnly=true)
    	public T get(BigDecimal id) {
    		log.debug("getting instance with id: " + id);
    		if (id == null){
    			return null;
    		} 
    		else{
    			try {			
    				return entityManager.find(type, id);
    			} catch (RuntimeException re) {
    				log.error("get failed", re);
    				throw re;
    			}
    		}		
    	}
    	
    	@Override
    	//@Transactional(readOnly=true)
    	public T get(int id) {
    		log.debug("getting instance with id: " + id);
    		if (id == 0){
    			return null;
    		} 
    		else{
    			try {			
    				return entityManager.find(type, id);
    			} catch (RuntimeException re) {
    				log.error("get failed", re);
    				throw re;
    			}
    		}		
    	}
    
    	@Override
    	//@Transactional(readOnly=true)
    	public List<T> getAll() {
    		return entityManager.createQuery("select o from " + 
    				type.getName() + " o").getResultList();				
    	}
    
    	@Override
    	//@Transactional(propagation = Propagation.REQUIRES_NEW)
    	public void save(T object) {
    		log.debug("persisting TiesEmployees instance");
    		try {
    			entityManager.persist(object);
    			log.debug("persist successful");
    		} catch (RuntimeException re) {
    			log.error("persist failed", re);
    			throw re;
    		}		
    	}
    
    	@Override
    	public void delete(T object) {
    		entityManager.remove(object);
    	}
    
    	@Override
    	public T merge(T object) {
    		log.debug("merging instance");
    		try {
    			return entityManager.merge(object);
    		} catch (RuntimeException re) {
    			log.error("merge failed", re);
    			throw re;
    		}
    	}
    
    }
    Last edited by davestar; Mar 7th, 2011, 06:31 PM. Reason: adding persistence.xml

  • #2
    Were you able to find solution to the problem ? I am facing the exact same issue. My read works but update and inserts do not. If I try to insert and update outside of a JTA, it works. For example a junit works. I see the below logs in hibernate and spring.

    2011-09-15 12:58:27.876 [WebContainer : 0] DEBUG o.h.e.StatefulPersistenceContext [StatefulPersistenceContext.java:892] initializeNonLazyCollections# - initializing non-lazy collections
    2011-09-15 12:58:27.876 [WebContainer : 0] DEBUG o.h.e.AbstractEntityManagerImpl [AbstractEntityManagerImpl.java:934] joinTransaction# - Looking for a JTA transaction to join
    2011-09-15 12:58:27.876 [WebContainer : 0] WARN o.h.e.AbstractEntityManagerImpl [AbstractEntityManagerImpl.java:1043] joinTransaction# - Cannot join transaction: do not override hibernate.transaction.factory_class
    2011-09-15 12:58:27.876 [WebContainer : 0] DEBUG o.s.o.j.ExtendedEntityManagerCreator$ExtendedEntit yManagerInvocationHandler [ExtendedEntityManagerCreator.java:383] doJoinTransaction# - Joined JTA transaction
    2011-09-15 12:58:27.876 [WebContainer : 0] DEBUG o.h.e.AbstractEntityManagerImpl [AbstractEntityManagerImpl.java:934] joinTransaction# - Looking for a JTA transaction to join
    2011-09-15 12:58:27.876 [WebContainer : 0] WARN o.h.e.AbstractEntityManagerImpl [AbstractEntityManagerImpl.java:1043] joinTransaction# - Cannot join transaction: do not override hibernate.transaction.factory_class
    2011-09-15 12:58:27.876 [WebContainer : 0] DEBUG o.s.o.j.ExtendedEntityManagerCreator$ExtendedEntit yManagerInvocationHandler [ExtendedEntityManagerCreator.java:383] doJoinTransaction# - Joined JTA transaction
    2011-09-15 12:58:27.876 [WebContainer : 0] DEBUG o.h.e.AbstractEntityManagerImpl [AbstractEntityManagerImpl.java:934] joinTransaction# - Looking for a JTA transaction to join
    2011-09-15 12:58:27.876 [WebContainer : 0] WARN o.h.e.AbstractEntityManagerImpl [AbstractEntityManagerImpl.java:1043] joinTransaction# - Cannot join transaction: do not override hibernate.transaction.factory_class
    2011-09-15 12:58:27.876 [WebContainer : 0] DEBUG o.s.o.j.ExtendedEntityManagerCreator$ExtendedEntit yManagerInvocationHandler [ExtendedEntityManagerCreator.java:383] doJoinTransaction# - Joined JTA transaction
    2011-09-15 12:58:27.892 [WebContainer : 0] DEBUG o.h.e.AbstractEntityManagerImpl [AbstractEntityManagerImpl.java:934] joinTransaction# - Looking for a JTA transaction to join
    2011-09-15 12:58:27.892 [WebContainer : 0] WARN o.h.e.AbstractEntityManagerImpl [AbstractEntityManagerImpl.java:1043] joinTransaction# - Cannot join transaction: do not override hibernate.transaction.factory_class
    2011-09-15 12:58:27.892 [WebContainer : 0] DEBUG o.s.o.j.ExtendedEntityManagerCreator$ExtendedEntit yManagerInvocationHandler [ExtendedEntityManagerCreator.java:383] doJoinTransaction# - Joined JTA transaction

    Comment


    • #3
      Yes, move the @Transactional(propagation = Propagation.REQUIRES_NEW) annotation to the service class. Annotating at the DAO level does not seem to have any effect.
      Please let me know if it worked.

      Comment

      Working...
      X