Spring中的AOP,以及声明式事务 @Transactional无法拦截事务

Sou2012 2008-12-20 05:32:18
加精
DAO中
@Repository
public class TestDao extends BaseHibernateDao {

public void test(){
String hql = " update ZTest set uname = ? where id = 5 ";
Query q = this.getSession(true).createQuery(hql);
q.setString(0, "x");
q.executeUpdate();

String hql2 = " update ZTest set unadfme = ? where id = 5";
Query q2 = this.getSession(true).createQuery(hql2);
q2.executeUpdate();
}

}


SERVICE中

@Service
public class ServiceTest extends BaseService {

@Autowired
TestDao testDao;

@Override
public void destroy() {

}

@Override
public void init() {

}


@Transactional
public void testTransactional(){
testDao.test();
}

}

CONTROLLER中

@Controller
public class PLetterController extends BaseController {
@Autowired
ServiceTest serviceTest;


@RequestMapping("/testT.do")
public void test(Writer writer) throws Exception{
serviceTest.testTransactional();
}

}

SPRING 管理 HIBERNATE

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:省略..........</value>
</property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>com.XXXXXXX.domain</value>
</list>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation">
<value>WEB-INF/classes/hibernate.cfg.xml</value>
</property>
</bean>

<!--
<tx:annotation-driven transaction-manager="transactionManager" />

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

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />

<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor" ref="transactionInterceptor"/>
</bean>

<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributeSource">
<bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
</property>
</bean>
-->

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>


</beans>

大侠救命!
...全文
7388 76 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
76 条回复
切换为时间正序
请发表友善的回复…
发表回复
hhp50215 2012-05-20
  • 打赏
  • 举报
回复
感谢,遇到同一个问题。虽然不知道哪边多加了个OpenSessionInView...但明天还是研究下
kitajima-- 2011-07-26
  • 打赏
  • 举报
回复
xiaomiao0815 2011-05-07
  • 打赏
  • 举报
回复
学习了!
pengpzy 2008-12-26
  • 打赏
  • 举报
回复
好呀!
thinkingmysky 2008-12-25
  • 打赏
  • 举报
回复
mark
cyfxbyfx 2008-12-25
  • 打赏
  • 举报
回复
楼主伟大!支持!顺便接分!
fzhemingren 2008-12-25
  • 打赏
  • 举报
回复
路过
yanbeifei168 2008-12-25
  • 打赏
  • 举报
回复
帮顶
lzp765 2008-12-25
  • 打赏
  • 举报
回复
mark
jiejie340624640 2008-12-25
  • 打赏
  • 举报
回复
很好
  • 打赏
  • 举报
回复
俺也刚开始学AOP,也遇上过这样的问题,正在等待大侠们帮忙解决……
yuer2084 2008-12-24
  • 打赏
  • 举报
回复
关注一下
Sou2012 2008-12-23
  • 打赏
  • 举报
回复
[Quote=引用 52 楼 Landor2004 的回复:]
为什么是getJdbcTemplate,不是hibernateTemplate吗?
JdbcTemplate不使用的HibernateTransactionManager呀,用的是DataSourceTransactionManager

有点乱呀,呵呵,你根本没加入事务!
[/Quote]

我是没折了,把各种Manager排列组合试一下,真是郁闷,

搞了4天了。。。
Landor2004 2008-12-23
  • 打赏
  • 举报
回复
为什么是getJdbcTemplate,不是hibernateTemplate吗?
JdbcTemplate不使用的HibernateTransactionManager呀,用的是DataSourceTransactionManager

有点乱呀,呵呵,你根本没加入事务!
dysfgjl 2008-12-23
  • 打赏
  • 举报
回复
hao tie jiafena
yougucao379548695 2008-12-23
  • 打赏
  • 举报
回复
我以前也遇到过。
Sou2012 2008-12-23
  • 打赏
  • 举报
回复
[Quote=引用 44 楼 Landor2004 的回复:]
怎么会不起作用呢,我的测试代码如下,测试通过。
spring版本:2.5,用声明式事务
applicationContext.xml:

XML code<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
......
<context:component-scan base-package="com.huaxia.oaapp">
<context:include-filter type="aspectj" expression="com.huaxia.oaapp.service..*"/>
<contex…
[/Quote]
为什么我这边还是不行

DAO中
public void test(){
String param = "sdfsdfsdf";
String sql = " update z_test set uname = '" + param + "' where id = 5 ";
this.getJdbcTemplate().queryForList(sql);

String sql2 = " update z_test set unamfe = 'sdfsdfsd' where id = 5 ";
this.getJdbcTemplate().queryForList(sql2);
}

我在service中调了这个方法

那个sql2中 unamfe在数据库是没有的字段,执行的时候会抛异常,

照理说,事务会回滚的,但是我在数据库还是看到了 id为5的 uname字段改成了 sdfsdfsdf


我的配置文件

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config>
<aop:pointcut id="baseServiceMethods"
expression="execution(* com.xx.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="baseServiceMethods" />
</aop:config>
<!-- <aop:aspectj-autoproxy /> -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="select*" read-only="true" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="add*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="delete*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
</tx:attributes>
</tx:advice>
PS:

我的SPRINGMVC 放在 dispatch-servlet.xml

hibernate放在 spring-hibernate.xml

spring security 放在 spring-security.xml

这样配置是不是有问题?
hero_shaoshuai 2008-12-23
  • 打赏
  • 举报
回复
关注关注啦
derelictangel 2008-12-23
  • 打赏
  • 举报
回复
接..分...


PS:
我的目标是 ---->

^_^
derelictangel 2008-12-23
  • 打赏
  • 举报
回复
接分


PS:
我的目标是 ---->

^_^
加载更多回复(54)

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧