初学者,参照别人的经验,配置文件如下 <!-- 配置切面 -->
<aop:config proxy-target-class="true" >
<aop:pointcut id="servicePointcut" expression="execution(* com.orm.hr.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
这样的话,运行正常没有问题,事务都能提交,但是,这样切面是在dao层,一些涉及到多个crud操作的service层的方法就不好处理。我想把切面放在service层,应该怎么做?我尝试直接修改execution为(* com.orm.hr.service.*.*(..)),也能运行,但是事务不提交。
<aop:config proxy-target-class="true" >
<aop:pointcut id="servicePointcut" expression="execution(* com.orm.hr.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
这样的话,运行正常没有问题,事务都能提交,但是,这样切面是在dao层,一些涉及到多个crud操作的service层的方法就不好处理。我想把切面放在service层,应该怎么做?我尝试直接修改execution为(* com.orm.hr.service.*.*(..)),也能运行,但是事务不提交。
解决方案 »
- 做定时器时的监听器 报错 严重: Error configuring application listener of class test.timer.Time
- javaweb初学者可以看看我们写的这个示例项目
- JSP页面数据只有部分插入数据库
- jdbc是如何进行事务处理的?
- 求人帮忙解析一下下面这段日志,用java实现??
- struts中如何实现在表单提交中文后,在后台操作时会自动把提交的中文映射成对应编号!?
- 一个关于JTree的用法问题,请高手指点迷津
- 关于eclipse开发ejb弱中弱的问题...很急.....
- 用Java怎么实现CAD技术?
- 如何搭建EJB?吐雪放分
- 简单的jsp输出
- MySql Hibernate 查询语句问题
com.orm.hr.service包下面就一个类EmployeeService,这个类就一个方法login
public Employee login(String username, String password, int departmentId) throws LoginException{
Employee employee = this.employeeDao.getEmployeeByLogin(username, password);
if(employee == null){
throw new LoginException("工号或密码错误。");
}else{
if(employee.getDepartment().getId() != departmentId){
throw new LoginException("该工号不在所选择的部门。");
}else{
employee.setLoginCount(employee.getLoginCount() + 1);
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
employee.setLoginTime(c);
this.employeeDao.updateEmployee(employee);
return employee;
}
}
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
http://hi.baidu.com/freshman0502/blog/item/8068e01b5038d01c8618bf8e.html
改成
* execution(* com.orm.hr.dao.*.*(..))
表示任意方法的任意返回值
在
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
里面<tx:method name="*" read-only="true"/>
配置前加一个
<tx:method name="login*" propagation="REQUIRED" rollback-for="Exception"/>
如果不加这个的话login方法为只读的,所以你不能修改数据,事物没提交了
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="login*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>