0

我正在尝试使用使用 AOP 和休眠的 Spring 事务管理来实现示例应用程序。下面是我的配置文件

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

这是我的 impl 课程

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

}

当我运行应用程序时,我在日志中看到以下行,这表明我的 spring 事务没有被调用。

2013 年 2 月 27 日上午 11:00:20 org.hibernate.dialect.Dialect 信息:HHH000400:使用方言:org.hibernate.dialect.PostgreSQLDialect 2013 年 2 月 27 日上午 11:00:20 org.hibernate.engine.jdbc.internal .LobCreatorBuilder useContextualLobCreation INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reports JDBC version [3] less than 4 Feb 27, 2013 11:00:20 AM默认事务策略(直接 JDBC 事务)2013 年 2 月 27 日上午 11:00:20 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory 信息:HHH000397:使用 ASTQueryTranslatorFactory

我错过了什么吗?请帮忙

4

1 回答 1

1

来自 vmoksha 的 Sunilkumar

将此行放入您的 applicationcontext.xml

< tx:注解驱动 />


并在您的班级中也添加注释 @Transaction

于 2013-02-27T12:24:55.953 回答