C#  DAL层代码,运行多条增删改,使用事务操作:

 /// <summary>
/// 运行 多条增删改 (非查询语句)
/// </summary>
/// <param name="strSql"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static int ExcuteNonQuerys(string[] strSqls, SqlParameter[][] paras2Arr)
{
int res = 0;
//创建连接通道
using (SqlConnection conn = new SqlConnection(strConn))
{
conn.Open();
//创建 事务
SqlTransaction tran = conn.BeginTransaction();
//创建命令对象
SqlCommand cmd = new SqlCommand();
//为命令对象指定连接通道
cmd.Connection = conn;
//为命令对象指定事务
cmd.Transaction = tran;
try
{
//循环运行sql语句
for (int i = 0; i < strSqls.Length; i++)
{
//获得要运行的sql语句
string strSql = strSqls[i];
//为命令对象指定 此次运行的 sql语句
cmd.CommandText = strSql;
//加入參数
if (paras2Arr.Length > i)//假设 參数2维数组的长度大于当前循环的下标
{
cmd.Parameters.AddRange(paras2Arr[i]);//将 交错数组 的第一个元素(事实上也是一个数组,加入到參数集合中)
}
res += cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
tran.Commit();//提交事务
}
catch (Exception ex)
{
res = 0;
tran.Rollback();//回滚事务
throw ex;
}
}
return res;
}

JAVA  DAO层中编写事务代码:

@Test
public void test(){
Connection conn = null;
PreparedStatement stmt = null;
Savepoint sp = null;
try{
conn = JdbcUtil.getConnection();
conn.setAutoCommit(false); //开启事务
stmt = conn.prepareStatement("update account set money=money-100 where name='aaa'");
stmt.executeUpdate(); stmt = conn.prepareStatement("update account set money=money+100 where name='bbb'");
stmt.executeUpdate(); sp = conn.setSavepoint();//设置回滚点 stmt = conn.prepareStatement("update account set money=money-100 where name='bbb'");
stmt.executeUpdate();
int i=1/0; //bbb给ccc转账时遇到异常
stmt = conn.prepareStatement("update account set money=money+100 where name='ccc'");
stmt.executeUpdate(); }catch(Exception e){
e.printStackTrace();
try {
conn.rollback(sp); //回滚事务
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally{
try {
conn.commit(); //事务提交
} catch (SQLException e) {
e.printStackTrace();
}
JdbcUtil.release(null, stmt, conn);
}
}

JAVA  Service层使用事务操作代码:

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory;
//把得到连接及事务有关的方法写到此类中
public class TransactionUtil {
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); private static DataSource ds;
static{
try {
InputStream in = DBCPUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
Properties props = new Properties();
props.load(in);
ds = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataSource getDataSource(){
return ds;
}
public static Connection getConnection(){
try {
Connection conn = tl.get();
if(conn==null){
conn = ds.getConnection();
tl.set(conn);
}
return conn;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void startTransaction(){
try {
Connection conn = tl.get();
if(conn==null){
conn = getConnection();
// tl.set(conn);
}
conn.setAutoCommit(false);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void rollback(){
try {
Connection conn = tl.get();
if(conn==null){
conn = getConnection();
// tl.set(conn);
}
conn.rollback();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void commit(){
try {
Connection conn = tl.get();
if(conn==null){
conn = getConnection();
// tl.set(conn);
}
conn.commit();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void relase(){
try {
Connection conn = tl.get();
if(conn!=null){
conn.close();
tl.remove();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}

最新文章

  1. 深入理解javascript原型和闭包(10)——this
  2. main方法的理解
  3. C# 之 静态字段初始化
  4. UNIX Filesystems - Evolution Design and Implementation.pdf
  5. Linux系统编程(26)——守护进程
  6. 自定义cell时,在宽的手机上显示太窄解决办法
  7. WindowsAPI 之 CreatePipe、CreateProcess
  8. ASP.NET Core中使用IOC三部曲(一.使用ASP.NET Core自带的IOC容器)
  9. c++(递归和堆栈)
  10. java导入excel时处理日期格式(已验证ok)
  11. Scrum Meeting 博客
  12. Jenkins job之间依赖关系配置(联动构建)
  13. 第7章 贡献 - Identity Server 4 中文文档(v1.0.0)
  14. 动态库 Framework
  15. android targetSdkVersion&gt;=26收不到广播的处理
  16. ThinkPHP通过类的链式继承优化空操作的实现
  17. MySQL存储过程中的3种循环【转载】
  18. HDU 3378
  19. h3c 云计算管理平台
  20. 【NOIP模拟赛】黑红树 期望概率dp

热门文章

  1. BZOJ 2592 随机化(伪)
  2. ClouderaManager与CDH
  3. [ ZJOI 2006 ] Mahjong
  4. 【转】Centos7 ftp 配置及报错处理
  5. Linux学习(一)--基本概念
  6. HDU_1160_FatMouse&#39;s Speed_dp
  7. PHP 之获取Windows下CPU、内存的使用率
  8. xamarin.forms模拟rem动态大小值,实现屏幕适配
  9. On branch master nothing to commit, working tree clean ERROR: Repository not found. fatal: Could not read from remote repository.
  10. 踩过的坑:__file__、__package__和__name__