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>

大侠救命!
...全文
7379 76 打赏 收藏 转发到动态 举报
写回复
用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)
第十章 开发Spring应用... 189

10.1 简介... 189

10.1.1 Spring简介... 189

10.1.2 MyEclipse的Spring开发功能简介... 191

10.2 开发简单的Spring应用... 191

10.2.1 给项目加入Spring功能... 191

10.2.2 创建Bean类和配置信息... 193

10.2.3 Spring Beans 视图和Outline视图... 197

10.2.4 运行和测试... 199

10.3 开发Spring 1.2 AOP应用... 200

10.3.1 开发Man对象... 201

10.3.2 开发前置通知(Before advice)对象:FBI 201

10.3.3 装配拦截器和Bean. 202

10.3.4 测试和运行... 203

10.3.5 AOP简介和相关概念... 204

10.4 开发 Spring 2.0 AOP 应用... 206

10.4.1 使用aop 标签实现AOP. 206

10.4.2 使用标注(@AspectJ)实现AOP. 208

10.4.3 开发环绕通知(Around Advice)AOP 应用... 210

10.5 Spring数据库开发... 214

10.5.1 DataSource 和 JDBCTemplate开发... 214

10.5.2 Hibernate 整合 Spring开发... 223

10.5.2.1 创建项目,添加必要的开发功能... 223

10.5.2.2 反向工程生成Spring整合Hibernate的DAO.. 225

10.5.2.3 用Spring 1.2 的事务代理类解决事务提交问题... 232

10.5.2.4 用Spring 2.0 的aop和tx声明式配置解决事务提交问题... 234

10.5.2.5 用Spring 2.0 的@Transactional标注解决事务提交问题(最佳方案) 238

10.5.2.6 使用 HibernateTemplate 实现分页查询... 241

10.6 小结... 242

10.7 参考资料... 242

10.7.1 MyEclipse生成的Spring+Hibernate无法保存数据问题的解决方法... 242

10.7.2 MyEclipse生成的Spring+Hibernate无法保存数据问题的解决方法2 - 用 CGLIB 来实现事务管理 245

10.7.3 Spring相关的参考资料... 248

81,092

社区成员

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

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