接口:public interface UserService {
public void addUser(String username, String password);
public void deleteUser(int id);
public void updateUser(int id, String username);
public List queryUser(String username);
} 接口的实现:public class UserServiceImpl implements UserService {
private DataSource dataSource;
public void addUser(String username, String password) {
//正确的sql
String sql = "insert into aaa (t1,t2,t3) values (seq_food.nextval,'"+username+"','"+password+"')";
System.out.println(sql);
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
jdbcTemplate.update(sql);
}
public void deleteUser(int id) {
}
public void updateUser(int id, String username) {
//错误的sql,测试回滚
String sql = "update aa set t2='"+username+"' where t1='"+id+"'";
System.out.println(sql);
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
jdbcTemplate.update(sql);
}
public List queryUser(String username) {
return null;
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}测试servlet:public class UserServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
try {
UserService userService = (UserService) ctx.getBean("userService");
userService.addUser("1", "2");
userService.updateUser(10032190, "zf");
} catch (DataAccessException e) {
e.printStackTrace();
}
}
}spring配置文件:<?xml version="1.0" encoding="GB2312"?>
<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.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>proxool.foodManage</value>
</property>
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务管理 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="query*" propagation="REQUIRED" read-only="true" rollback-for="Exception"/>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="userServiceOperation" expression="execution(* cn.com.xy.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="userServiceOperation"/>
</aop:config>
<bean id="userService" class="cn.com.xy.imp.UserServiceImpl">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>做了一个简单的测试,使用上述的配置和测试代码,运行后,没有更新成功,但还是插入了一条新数据,事务回滚没有起作用,正确的应该是既不更新也不插入,编程式事务测试是好的,但是声明式事务就不对了,可能spring的配置不对,还望大家帮忙看看。
public void addUser(String username, String password);
public void deleteUser(int id);
public void updateUser(int id, String username);
public List queryUser(String username);
} 接口的实现:public class UserServiceImpl implements UserService {
private DataSource dataSource;
public void addUser(String username, String password) {
//正确的sql
String sql = "insert into aaa (t1,t2,t3) values (seq_food.nextval,'"+username+"','"+password+"')";
System.out.println(sql);
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
jdbcTemplate.update(sql);
}
public void deleteUser(int id) {
}
public void updateUser(int id, String username) {
//错误的sql,测试回滚
String sql = "update aa set t2='"+username+"' where t1='"+id+"'";
System.out.println(sql);
JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
jdbcTemplate.update(sql);
}
public List queryUser(String username) {
return null;
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}测试servlet:public class UserServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
try {
UserService userService = (UserService) ctx.getBean("userService");
userService.addUser("1", "2");
userService.updateUser(10032190, "zf");
} catch (DataAccessException e) {
e.printStackTrace();
}
}
}spring配置文件:<?xml version="1.0" encoding="GB2312"?>
<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.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.logicalcobwebs.proxool.ProxoolDriver</value>
</property>
<property name="url">
<value>proxool.foodManage</value>
</property>
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务管理 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="query*" propagation="REQUIRED" read-only="true" rollback-for="Exception"/>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="userServiceOperation" expression="execution(* cn.com.xy.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="userServiceOperation"/>
</aop:config>
<bean id="userService" class="cn.com.xy.imp.UserServiceImpl">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>做了一个简单的测试,使用上述的配置和测试代码,运行后,没有更新成功,但还是插入了一条新数据,事务回滚没有起作用,正确的应该是既不更新也不插入,编程式事务测试是好的,但是声明式事务就不对了,可能spring的配置不对,还望大家帮忙看看。
解决方案 »
- 如何将ASCII编码RSA公钥转换为publickey对象``?
- 谁帮我完善下代码啊?关于多线程下载的
- 解决java版本不匹配问题
- db2多次删除记录时候,会产生大量的日志,如何删除记录又不记录日志
- 请教一个org.apache.commons.net.ftp.FTPClient的问题!!急!
- 我学JAVA.求高手指点迷津!高分感激!!!!
- eclipse2.1.2可以运行,3.0m6,m7不能运行,出现错误,谁碰到过,请帮忙
- JBuilder9创建CMP 连接SQL Server的问题,请帮忙
- 大数据处理,有办法没?
- 急:IOS 用 URL 调用 外部接口
- JSF 页面用到frameset 跳转问题?
- JAVA高手进
userService.updateUser(10032190, "zf"); 这两个方法放到了action中,事务的层面是service,你这么做相当于启动了两个完全独立的事务
逻辑不对,呵呵