jdbc+spring+mysql事务理解和分析
首先了解事务的特性
事务四大特性(简称ACID)?
1、原子性(Atomicity):事务中的全部操作在数据库中是不可分割的,要么全部完成,要么均不执行。
2、一致性(Consistency):几个并行执行的事务,其执行结果必须与按某一顺序串行执行的结果相一致。
3、隔离性(Isolation):事务的执行不受其他事务的干扰,事务执行的中间结果对其他事务必须是透明的。
4、持久性(Durability):对于任意已提交事务,系统必须保证该事务对数据库的改变不被丢失,即使数据库出现故障。
?
在Action层(servlet)
依赖注入signService,调用saveSignInfo方法。
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { signService.saveSignInfo(username, password,nickname,pic_url,remark); } catch (RuntimeException e) { e.printStackTrace(); }}?
-------------------------------
在Service层接口定义
public interface ISignService { void saveSignInfo(SignBean sign);}?//实现类,测试事务,方法命名:save开头
public class SignServiceImpl implements ISignService { //模拟数据库表中id为自增长的主键列,假设第一条添加成功,第二条添加失败,测试事务回滚。 public int saveSignInfo(String username, String password, String nickname, String pic_url, String remark) { SignBean sign = new SignBean(); sign.setId(25); sign.setUser_id(6); sign.setMood("juint测试啦ww"); signDao.saveQq(sign); System.out.println("333333333333333"); SignBean sign2 = new SignBean(); sign2.setId(24); sign2.setUser_id(2); sign2.setMood("juint测试啦qq"); signDao.saveQq(sign2); System.out.println("44444444444444"); }}?在Dao层接口定义
public interface ISignDao { void saveQq(SignBean sign);}?实现类,继承JdbcDaoSupport 。
public class SignDaoJdbc extends JdbcDaoSupport implements ISignDao { public void saveQq(SignBean sign) { String sql = " insert into tbl_sign(id,user_id,mood,save_time) " + " values("+sign.getId()+","+sign.getUser_id()+",'"+sign.getMood()+"',now()) "; JdbcTemplate jt=this.getJdbcTemplate(); jt.execute(sql); }}?
applicationContext.xml配置文件
?
<bean id="dataSource" ref="dataSource" /> </bean> <bean id="txProxyTemplate" abstract="true" autowire="byName" ref="transactionManager" /> <property name="transactionAttributes"> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <bean id="signDao" ref="dataSource"/> </bean> <bean id="signService" parent="txProxyTemplate"> <property name="target"> <bean ref="signDao" /> </bean> </property> </bean>
?说明:
1.由于id键重复导致一条添加成功,一条失败.报错信息如下
org.springframework.dao.DataIntegrityViolationException: StatementCallback; SQL [ insert into tbl_sign(id,user_id,mood,save_time)? values(24,2,'juint测试啦qq4444',now()) ]; Duplicate entry '24' for key 1; nested exception is java.sql.SQLException: Duplicate entry '24' for key 1
Caused by: java.sql.SQLException: Duplicate entry '24' for key 1
从而达到测试回滚效果。
?
2. dao,service不用throws Exception;???? mysql类型必须为InnoDB才支持事务。
?
3. --service方法名的问题-- 以save开头就可以了(以PROPAGATION_REQUIRED定义的为准),否则其他的开头方法名报错信息如下:
org.springframework.jdbc.UncategorizedSQLException: StatementCallback; uncategorized SQLException for SQL [ insert into tbl_sign(id,user_id,mood,save_time)? values(5,4,'juint测试啦qq',now()) ]; SQL state [S1009]; error code [0]; Connection is read-only. Queries leading to data modification are not allowed.; nested exception is java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed.
Caused by: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed.
?
4. dao,service都不try ... catch 异常,在action层才捕获异常,会逐层往外抛出异常的。
?
5. dao方法名跟事务没有关系。
?