alromp
alromp

Reputation: 1

Spring declarative transaction using AOP with hibernate, still using the default transaction strategy

I'm trying to implement a sample app using Spring transaction management using AOP and hibernate. Below is my config file

     <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd ">


    <bean id="txManager"  
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
      <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- Beans Declaration -->
    <bean id="userDAO" class="com.wjb.daoimpl.UserDAOImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

         <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
           <tx:method name="add*" read-only="true"/>
           </tx:attributes>
         </tx:advice>

         <aop:config>
           <aop:pointcut id="userOperation" expression="execution(* com.wjb.daoimpl.UserDAOImpl.*(..))"/>
         <aop:advisor advice-ref="txAdvice" pointcut-ref="userOperation"/>
         </aop:config>


<!-- Database Configuration -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://10.12.30.180:4568/finitedb" />
<property name="username" value="finiteuser" />
<property name="password" value="finiteuser" />
</bean>

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="annotatedClasses">
            <list>
                <value>com.wjb.user.model.User1</value>
            </list>
        </property>

    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>

    <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
         <prop key="hibernate.show_sql">true</prop>
       </props>
    </property>
 </bean>


</beans>

and this is my impl class

public class UserDAOImpl {

    Session session;
    SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory){
        this.sessionFactory = sessionFactory;
        session = this.sessionFactory.openSession();
    }

    public void addUser(User1 user) {
        session.save(user);
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

When I run the application, I'm seeing the below lines in the log which shows that my spring transaction is not being invoked.

Feb 27, 2013 11:00:20 AM org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect Feb 27, 2013 11:00:20 AM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 Feb 27, 2013 11:00:20 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) Feb 27, 2013 11:00:20 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory INFO: HHH000397: Using ASTQueryTranslatorFactory

Am I missing anything? Please help

Upvotes: 0

Views: 1699

Answers (1)

Sunilkumar from vmoksha

put this line in to your applicationcontext.xml

< tx:annotation-driven />


and also put annotaion in your class also @Transaction

Upvotes: 1

Related Questions