Results 1 to 6 of 6

Thread: Sprinng doesnt intercept transaction

  1. #1
    Join Date
    Jul 2010
    Posts
    22

    Default 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> {
    ...
    }
    Attachment 4596
    Last edited by emorozovs; Jan 25th, 2012 at 02:31 AM. Reason: inserting image

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

    Default

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

  3. #3
    Join Date
    Jul 2010
    Posts
    22

    Default

    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();	
    	}

  4. #4
    Join Date
    Jul 2010
    Posts
    22

    Default

    Here are screens from profiler, hope readable..
    Attachment 4596
    Last edited by emorozovs; Jan 25th, 2012 at 02:33 AM.

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

    Default

    Which is where your problem is. I suggest a read of the reference guide. Don't use a BeanFactory use an ApplicationContext.
    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

  6. #6
    Join Date
    Jul 2010
    Posts
    22

    Default

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

    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."
    Last edited by emorozovs; Jan 25th, 2012 at 04:09 AM.

Posting Permissions

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