Announcement Announcement Module
Collapse
No announcement yet.
Problem with declarative transaction management Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with declarative transaction management

    I am facing some problem with TransactionProxyFactoryBean.When i am doing the transaction rollback also it is saving......
    My code is:

    applicationContext.xml

    <bean id="userdao" class="com.impl.UserDAOImpl">
    <property name="sessionFactory">
    <ref local="sesfactory1" />
    </property>
    </bean>
    <bean id="userManagerTarget" class="com.impl.UserManagerImpl">
    <property name="userDAO">
    <ref local="userdao" />
    </property>
    </bean>
    <bean id="transactionManager1"
    class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory">
    <ref local="sesfactory1" />
    </property>
    </bean>
    <bean id="userManager"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref local="transactionManager1" />
    </property>
    <property name="target">
    <ref local="userManagerTarget" />
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="getUserList">
    PROPAGATION_REQUIRES_NEW,-com.Testexception
    </prop>
    <prop key="insert*">
    PROPAGATION_REQUIRED,-com.Testexception
    </prop>

    </props>
    </property>
    </bean>



    UserDAOImpl.java

    public List getList()
    {
    AgUser aguser=new AgUser();
    aguser.setCity("rvp");
    aguser.setName("Naren");
    aguser.setDob(new java.util.Date());
    aguser.setZipcode(new Long(234));
    //System.out.println(getHibernateTemplate());
    getHibernateTemplate().save(aguser);
    System.out.println("After saving...");

    Session ses=getSession();
    l1=ses.createCriteria(AgUser.class).list();
    System.out.println(l1.size());


    return null;

    }

    UserManagerImpl.java


    public List getUserList() throws com.Testexception
    {
    List l1=null;

    try
    {
    l1=userdao.getList();
    System.out.println("size in UsermanagerImpl---"+l1.size());

    }
    catch(Exception e)
    {
    System.out.println("----->"+e.getMessage());
    //e.printStackTrace();
    throw new com.Testexception();

    }


    Client Program

    ApplicationContext ctx =
    new ClassPathXmlApplicationContext(new String []{"applicationContext.xml"});

    UserManagerImpl um=(UserManagerImpl)ctx.getBean("userManagerTarget ");
    List l2=null;
    try
    {

    l2=um.getUserList();
    }
    catch(Exception e)
    {
    System.out.println("No Error");
    }



    even though i am raising Testexception explicitly the recording is saving successfully..... what is wrong in my code

  • #2
    Please use [ code][/code ] tags when posting code, that way we can more easily read your code.

    Is your UserDaoImpl extending HibernateDaoSupport or do you have your own implementation...

    Just noticed your problem, you are using the wrong bean.

    Code:
    UserManagerImpl um=(UserManagerImpl)ctx.getBean("userManagerTarget");
    You must use the userManager that is the instance that is proxied and has transactions applied. This is exactly the reason why I always suggest to use an anonymous inner bean as the target, that way you cannot (easily) get access to the NON-proxied instance.

    Code:
    UserManagerImpl um=(UserManagerImpl)ctx.getBean("userManager");
    Code:
    <bean id="userdao" class="com.impl.UserDAOImpl">
      <property name="sessionFactory" ref="sesfactory1"/>
    </bean>  
    
    <bean id="transactionManager1" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
      <property name="sessionFactory" ref="sesfactory1"/>
    </bean>
    
    <bean id="userManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="transactionManager" ref="transactionManager1"/>
      <property name="target">
        <bean class="com.impl.UserManagerImpl">
          <property name="userDAO" ref="userDao"/>
        </bean>
      </property>
      <property name="transactionAttributes">
        <props>
          <prop key="getUserList">PROPAGATION_REQUIRES_NEW,-com.Testexception</prop>
          <prop key="insert*">PROPAGATION_REQUIRED,-com.Testexception</prop>
        </props>
      </property>
    </bean>
    The configuration above would have prevented you from even getting the non-proxied instance.
    Last edited by Marten Deinum; Jan 18th, 2008, 03:40 AM.

    Comment


    • #3
      yes. UserDAOImpl is extending HibernateDaoSupport.

      when i am doing


      [code]ApplicationContext ctx =
      new ClassPathXmlApplicationContext(new String []{"applicationContext.xml"});

      UserManagerImpl um=(UserManagerImpl)ctx.getBean("userManager");
      List l2=null;
      try
      {

      l2=um.getUserList();
      }
      catch(Exception e)
      {
      System.out.println("No Error");
      }[/code>

      i am getting following exception


      Exception in thread "main" java.lang.ClassCastException: $Proxy1
      at com.SimpleClient.main(SimpleClient.java:23)
      Last edited by narendraprakash; Jan 18th, 2008, 03:40 AM.

      Comment


      • #4
        You should be programming to interfaces and should cast it to UserManager instead of UserManagerImpl. If you do't programm to interfaces enable class proxying.

        Comment


        • #5
          Thanks You very much.

          when i am changing UserManager it is working fine.


          If i am not using interfaces how to enable proxy?

          Comment


          • #6
            If you don't use interfaces class proxying is enabled automatically, you can force it by setting the property proxyTargetClass which you need to set to true.

            Comment


            • #7
              UserManager um=(UserManager)ctx.getBean("userManager");

              even i am not raising Testexception also it is not saving into the database...

              what is the problem again?

              Comment


              • #8
                I strongly suggest you read the documentation. RuntimeException are rolled back always, for checked exceptions you have to specify what to do. This follows the EJB specifications.

                So if you use the same code it will throw a NullPointerException, which is a RuntimeException which will rollback the transaction.

                Comment

                Working...
                X