Announcement Announcement Module
Collapse
No announcement yet.
Sprinng doesnt intercept transaction Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sprinng doesnt intercept transaction

    Hi! Profiling application I see that spring doesn't intercept transaction. The DAO interface is marked with annotation @Transactional, so spring should create proxy from such beans and wrap calls in transaction. Neither I see proxies in profiler nor data is rolled back when throwing Runtime exception in transactional methods. Please help. Spring 3.x.
    Method doSomething() in service is @Transactional and all dao instance methods. Please help, what I configured wrong???
    Code:
    	..........
            <tx:annotation-driven/>
    	<tx:annotation-driven transaction-manager="transactionManager" />
    	
    
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    		.........
    	</bean>
    
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    		<property name="dataSource" ref="dataSource"></property>
    	</bean>
    
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    
    
    	<bean id="userDao" class="com.test.dao.UserDaoImpl">
    		<property name="sessionFactory" ref="sessionFactory"></property>
    	</bean>
    
    
    
    	<bean id="userService" class="com.test.service.UserServiceimpl">
    		<property name="userDao" ref="userDao" />
    	</bean>
            
             .......



    Code:
    public interface UserService {
    	User loadUserById(long userId);	
    	void doSomething();
    }
    
    
    public class UserServiceimpl implements UserService {
    	
    	@Transactional
    	@Override
    	public void doSomething() {
    		User user = loadUserById(1);
    		user.fillUpMoney(999);
    		userDao.update(user);
    		throw new RuntimeException("Should be rollback");
    	}
    
    .....
    }
    
    
    @Transactional
    public interface BaseDao<T> {
    ...
    }
    Last edited by emorozovs; Jan 25th, 2012, 03:31 AM. Reason: inserting image

  • #2
    Make sure that you don't have duplicate instances of your beans if your beans are also marked @Component (or one of the sub annotations) make sure you aren't scanning for these beans also. Also make sure you are using the instances from your context and aren't creating new instances at your own.

    As a last remark only your service layer should be transactional NOT your daos.

    Comment


    • #3
      I have configured beans only in xml, I didn't use @Component annotation. Also I retrieve bean from XmlBeanFactory.

      Code:
                 public static void main(String [] args) {
      		BeanFactory  factory = new XmlBeanFactory(new FileSystemResource("src/main/resources/app-config.xml"));
      		UserService userService = (UserService)factory.getBean("userService");
      		userService.doSomething();	
      	}

      Comment


      • #4
        Here are screens from profiler, hope readable..
        Last edited by emorozovs; Jan 25th, 2012, 03:33 AM.

        Comment


        • #5
          Which is where your problem is. I suggest a read of the reference guide. Don't use a BeanFactory use an ApplicationContext.

          Comment


          • #6
            Thank you Marten. You saved me time
            Here is screen from profiler also, to see how it should be internally for people with interest...
            Attachment

            From Spring reference:
            "Because the ApplicationContext includes all functionality of the BeanFactory, it is generally
            recommended over the BeanFactory, except for a few situations such as in an Applet where
            memory consumption might be critical and a few extra kilobytes might make a difference. However, for
            most typical enterprise applications and systems, the ApplicationContext is what you will want to
            use. Spring 2.0 and later makes heavy use of the BeanPostProcessor extension point (to effect
            proxying and so on). If you use only a plain BeanFactory, a fair amount of support such as
            transactions and AOP will not take effect, at least not without some extra steps on your part. This
            situation could be confusing because nothing is actually wrong with the configuration."
            Attached Files
            Last edited by emorozovs; Jan 25th, 2012, 05:09 AM.

            Comment

            Working...
            X