日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

java代码中添加事务_C#和JAVA中编写事务代码

發(fā)布時間:2025/3/21 C# 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java代码中添加事务_C#和JAVA中编写事务代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

C#??DAL層代碼,執(zhí)行多條增刪改,使用事務(wù)操作:

///

/// 執(zhí)行 多條增刪改 (非查詢語句)

///

///

///

///

public static int ExcuteNonQuerys(string[] strSqls, SqlParameter[][] paras2Arr)

{

int res = 0;

//創(chuàng)建連接通道

using (SqlConnection conn = new SqlConnection(strConn))

{

conn.Open();

//創(chuàng)建 事務(wù)

SqlTransaction tran = conn.BeginTransaction();

//創(chuàng)建命令對象

SqlCommand cmd = new SqlCommand();

//為命令對象指定連接通道

cmd.Connection = conn;

//為命令對象指定事務(wù)

cmd.Transaction = tran;

try

{

//循環(huán)執(zhí)行sql語句

for (int i = 0; i < strSqls.Length; i++)

{

//獲得要執(zhí)行的sql語句

string strSql = strSqls[i];

//為命令對象指定 此次執(zhí)行的 sql語句

cmd.CommandText = strSql;

//添加參數(shù)

if (paras2Arr.Length > i)//如果 參數(shù)2維數(shù)組的長度大于當前循環(huán)的下標

{

cmd.Parameters.AddRange(paras2Arr[i]);//將 交錯數(shù)組 的第一個元素(其實也是一個數(shù)組,添加到參數(shù)集合中)

}

res += cmd.ExecuteNonQuery();

cmd.Parameters.Clear();

}

tran.Commit();//提交事務(wù)

}

catch (Exception ex)

{

res = 0;

tran.Rollback();//回滾事務(wù)

throw ex;

}

}

return res;

}

JAVA??DAO層中編寫事務(wù)代碼:

@Test

public void test(){

Connection conn = null;

PreparedStatement stmt = null;

Savepoint sp = null;

try{

conn = JdbcUtil.getConnection();

conn.setAutoCommit(false); //開啟事務(wù)

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();//設(shè)置回滾點

stmt = conn.prepareStatement("update account set money=money-100 where name='bbb'");

stmt.executeUpdate();

int i=1/0; //bbb給ccc轉(zhuǎn)賬時遇到異常

stmt = conn.prepareStatement("update account set money=money+100 where name='ccc'");

stmt.executeUpdate();

}catch(Exception e){

e.printStackTrace();

try {

conn.rollback(sp); //回滾事務(wù)

} catch (SQLException e1) {

e1.printStackTrace();

}

}finally{

try {

conn.commit(); //事務(wù)提交

} catch (SQLException e) {

e.printStackTrace();

}

JdbcUtil.release(null, stmt, conn);

}

}

JAVA??Service層使用事務(wù)操作代碼:

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;

//把得到連接及事務(wù)有關(guān)的方法寫到此類中

public class TransactionUtil {

private static ThreadLocal tl = new ThreadLocal();

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);

}

}

}

總結(jié)

以上是生活随笔為你收集整理的java代码中添加事务_C#和JAVA中编写事务代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。