Announcement Announcement Module
No announcement yet.
Sprinng doesnt intercept transaction Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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???
    	<tx:annotation-driven transaction-manager="transactionManager" />
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    	<bean id="sessionFactory"
    		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    		<property name="dataSource" ref="dataSource"></property>
    	<bean id="transactionManager"
    		<property name="sessionFactory" ref="sessionFactory" />
    	<bean id="userDao" class="com.test.dao.UserDaoImpl">
    		<property name="sessionFactory" ref="sessionFactory"></property>
    	<bean id="userService" class="com.test.service.UserServiceimpl">
    		<property name="userDao" ref="userDao" />

    public interface UserService {
    	User loadUserById(long userId);	
    	void doSomething();
    public class UserServiceimpl implements UserService {
    	public void doSomething() {
    		User user = loadUserById(1);
    		throw new RuntimeException("Should be rollback");
    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.


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

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


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


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


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

            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.