Results 1 to 8 of 8

Thread: Configuration of Transaction with Spring

  1. #1
    Join Date
    Apr 2011
    Posts
    12

    Unhappy 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. #2

    Default

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

  3. #3
    Join Date
    Apr 2011
    Posts
    12

    Default

    Quote 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().

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Quote 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!!!

    Quote 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 at 05:59 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Apr 2011
    Posts
    12

    Default

    Quote 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.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    You must also remove the current_session_context from the configuration.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Apr 2011
    Posts
    12

    Default

    Quote 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

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •