Announcement Announcement Module
Collapse
No announcement yet.
Configuration of Transaction with Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Configuration of Transaction with Spring

    Hello friends, I am having some problem with configuring transaction with spring having Hibernate as the orm tool. My configurations are as follows...

    spring-transaction.xml
    Code:
    <tx:annotation-driven transaction-manager="txManager"/>
    
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    spring-persistance.xml
    Code:
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="com.ajaaxx.entities"/>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
    <prop key="hibernate.current_session_context_class">jta</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.connection.autocommit">false</prop>
    <prop key="hibernate.jdbc.batch_size">0</prop>
    <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
    </props>
    </property>
    </bean>


    Now in my repository I am doing this:
    Code:
    @Repository("repository")
    @Transactional
    public class AddCustomerRepositoryImpl implements AddCustomerRepository {
    	
    	Logger logger = Logger.getLogger(getClass());
    	
    	@Autowired
    	private SessionFactory sessionFactory;
    	
    	
    	
    	private  Session currentSession(){
    		Session session = sessionFactory.getCurrentSession();
    		logger.debug("Returning a new session in AddCustomerRepositoryImpl.");
    		return session;
    		}
    
    	@Override
    	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
    	public String addCustomer(KYC_Customer customer,
    			List<KYC_Document> documents) {
    		
    		logger.debug("Add customer initiated in AddCustomerRepositoryImpl. Customer to add : ["+customer+"]");
    		try{
    		Session session = currentSession();
    		session.beginTransaction();
    		session.save(customer);
    		for(KYC_Document doc:documents){
    			System.out.println("SAVING IN SESSION DOCUMENT : ["+doc+"]");
    			session.save(doc);
    		}
    		
    		//session.getTransaction().commit();
    		
    		}
    		catch(HibernateException he){
    			he.printStackTrace();
    			return REPOSITORY_RETURN_TYPES.EXCEPTION.toString();
    		}
    		
    		return REPOSITORY_RETURN_TYPES.SUCCESS.toString();
    		
    
    		
    	}}
    Now I am having an exception at the time of calling

    Code:
    sessionfactory.getCurrentSession()
    the exception is :

    Code:
    org.hibernate.HibernateException: No TransactionManagerLookup specified
    	at org.hibernate.context.JTASessionContext.currentSession(JTASessionContext.java:81)
    	at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:574)
    	at com.ajaaxx.repository.impl.AddCustomerRepositoryImpl.currentSession(AddCustomerRepositoryImpl.java:39)
    	at com.ajaaxx.repository.impl.AddCustomerRepositoryImpl.getServiceTypes(AddCustomerRepositoryImpl.java:93)
    	at com.ajaaxx.service.impl.AddCustomerServiceImpl.getServiceTypes(AddCustomerServiceImpl.java:37)
    	at com.ajaaxx.controllers.GeneratereportController.showForm(GeneratereportController.java:57)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    .....

    Where do I need to configure the TransactionManagerLookup.. Please help

  • #2
    Look at the following Link. You better try session.openSession then getSession.

    Comment


    • #3
      Originally posted by venkataprasad View Post
      Look at the following Link. You better try session.openSession then getSession.
      Sorry ... I did'nt get it. If I do openSession() then the flushing and closing of the session wold have to be done manually. I knew that after the transaction is complete, the transaction object itself commits the transaction enabling a single session to be persisted multiple method invocations, that is why we have to use getCurrentSession().

      Comment


      • #4
        Originally posted by vekataprasad
        Look at the following Link. You better try session.openSession then getSession.
        If you use spring managed transactions NEVER use openSession because this will give you an new session which isn't managed by spring!!!

        Originally posted by supratim
        Now I am having an exception at the time of calling

        Code:
        sessionfactory.getCurrentSession()
        Well not so strange... You configure hibernate to use JTA but in spring you configure a local hibernate transaction manager instead of JTA.. So either remove JTA from hibernate or configure a JTA transaction manager.

        Also your code is flawed, you want spring to manage the transaction for you then why on earth are you messing around with starting a transaction yourself?! Next to that you are catching the exception which basically renders spring managed transactions useless (it needs to see the exception to determine if it needs to rollback).
        Last edited by Marten Deinum; Sep 12th, 2011, 06:59 AM.

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          If you use spring managed transactions NEVER use openSession because this will give you an new session which isn't managed by spring!!!



          Well not so strange... You configure hibernate to use JTA but in spring you configure a local hibernate transaction manager instead of JTA.. So either remove JTA from hibernate or configure a JTA transaction manager.

          Also your code is flawed, you want spring to manage the transaction for you then why on earth are you messing around with starting a transaction yourself?! Next to that you are catching the exception which basically renders spring managed transactions useless (it needs to see the exception to determine if it needs to rollback).

          Thanks for the reply Marten. I have removed the JTATransactionFactory from hibernate configuration. But the error remains.

          Comment


          • #6
            You must also remove the current_session_context from the configuration.

            Comment


            • #7
              Originally posted by Marten Deinum View Post
              You must also remove the current_session_context from the configuration.
              After doing this I am getting a new exception:

              Code:
              org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

              Comment


              • #8
                I suggest the forum search for that as that question has been answered numerous times before...

                In short your transaction setup is wrong or missing.

                Comment

                Working...
                X