gpt4 book ai didi

java - Spring声明式事务使用AOP配合hibernate,仍然使用默认的事务策略

转载 作者:行者123 更新时间:2023-12-01 14:48:46 24 4
gpt4 key购买 nike

我正在尝试使用 AOP 和 hibernate 使用 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 事务没有被调用。

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

我错过了什么吗?请帮忙

最佳答案

来自 vmoksha 的 Sunilkumar

将此行放入您的 applicationcontext.xml

< tx:注解驱动/>

<小时/>

并且还在你的类中添加注释@Transaction

关于java - Spring声明式事务使用AOP配合hibernate,仍然使用默认的事务策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15105520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com