ibatis SqlMapClient进行事务管理

主要代码如下:

public Integer addXXX(XXX xxx) {
	Integer resId = 0;
	SqlMapClient client=getSqlMapClient();
	boolean autoCommit=false;
	Connection conn=null;
	try {
		client.startTransaction();
		conn=client.getCurrentConnection();
		autoCommit=conn.getAutoCommit();
		conn.setAutoCommit(false);
		resId = (Integer) client.insert("xxx",xxx);
		if(resId>0){
			YYY yyy=getYYY(resId,xxx);
			client.update("yyy",yyy);
		}else{
			throw new SQLException("add xxxx failed");
		}
		client.commitTransaction();
	} catch (Exception e) {
		resId = -1;
		try {
			client.getCurrentConnection().rollback();
		} catch (SQLException se) {
			se.printStackTrace();
		}
	}finally{
		try {
			conn.setAutoCommit(autoCommit);
			client.endTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	return resId;
}

 

你可能感兴趣的:(ibatis SqlMapClient进行事务管理)