Results 1 to 8 of 8

Thread: Problem with declarative transaction management

  1. #1

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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 at 03:40 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

  3. #3

    Default

    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 at 03:40 AM.

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

    Default

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

    Default

    Thanks You very much.

    when i am changing UserManager it is working fine.


    If i am not using interfaces how to enable proxy?

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

    Default

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

    Default

    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?

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

    Default

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

Posting Permissions

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