java代码中添加事务_C#和JAVA中编写事务代码
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 反射模式_java 设计模式—
- 下一篇: java表单自动绑定数据_java工作流