Oracle事务封装 TransactionHelper

封装事務,方便調用。

  1     public class TransactionHelper
  2     {
  3         public static OracleTransaction ora_Transaction = null;
  4         public static OracleCommand ora_Command { get; set; }
  5         public static OracleConnection ora_Conn = null;
  6 
  7         private static string ls_XMLFile = "SysInfo.xml";//配置文檔
  8         private static string ls_SectionName = "";
  9         private static Action initAction = () =>
 10         {
 11             #region 初始化Transaction
 12             if (string.IsNullOrEmpty(ls_SectionName)) ls_SectionName = "DBConn_EMES";
 13             ora_Command = new OracleCommand();
 14             ora_Conn = OracleHelper.DBOpen_Ora(ls_SectionName);
 15             ora_Conn.Open();
 16             ora_Command.Connection = ora_Conn;
 17             ora_Transaction = ora_Conn.BeginTransaction();
 18             ora_Command.Transaction = ora_Transaction;
 19             #endregion
 20         };
 21         private static Action disAction = () =>
 22         {
 23             #region 銷毀 Transaction
 24             if (ora_Command != null) ora_Command.Dispose();
 25             else ora_Command = null;
 26 
 27             if (ora_Transaction != null) ora_Transaction.Dispose();
 28             ora_Transaction = null;
 29             if (ora_Conn != null)
 30             {
 31                 if (ora_Conn.State == ConnectionState.Open)
 32                 {
 33                     ora_Conn.Close();
 34                 }
 35                 ora_Conn.Dispose();
 36             }
 37             ora_Conn = null;
 38             #endregion
 39         };
 40 
 41         public TransactionHelper()
 42         {
 43             try
 44             {
 45                 if (ora_Conn == null)
 46                 {
 47                     initAction.Invoke();
 48                 }
 49             }
 50             catch (Exception ex)
 51             {
 52                 StringBuilder lo_StringBuilder = new StringBuilder(ex.Message.ToString());
 53                 ECCHelper.ClassHelper.WriteLog(lo_StringBuilder);
 54                 disAction.Invoke();
 55             }
 56             //if (ora_Conn.State == ConnectionState.Open) ora_Conn.Close();
 57             //ora_Conn.Dispose();
 58 
 59         }
 60 
 61         public void executeNoneQueryCommand(string sqlCommand)
 62         {
 63             try
 64             {
 65                 ora_Command.CommandText = sqlCommand;
 66                 ora_Command.ExecuteNonQuery();
 67             }
 68             catch (Exception ex)
 69             {
 70                 StringBuilder lo_StringBuilder = new StringBuilder(ex.Message.ToString());
 71                 ECCHelper.ClassHelper.WriteLog(lo_StringBuilder);
 72                 disAction.Invoke();
 73                 throw ex;
 74             }
 75         }
 76 
 77         /// <summary>
 78         /// 初始調用 OracleCommnad 
 79         /// </summary>
 80         public OracleCommand InitCommand()
 81         {
 82             return ora_Command;
 83         }
 84 
 85         /// <summary>
 86         /// 提交&銷毀
 87         /// </summary>
 88         /// <returns></returns>
 89         public bool CommitTransaction()
 90         {
 91             try
 92             {
 93                 if (ora_Transaction != null)
 94                 {
 95                     ora_Transaction.Commit(); return true;
 96                 }
 97                 else { ora_Transaction.Rollback(); return false; }
 98             }
 99             catch (Exception ex)
100             {
101                 if (ora_Transaction != null) ora_Transaction.Rollback();
102                 StringBuilder lo_StringBuilder = new StringBuilder(ex.Message.ToString());
103                 ECCHelper.ClassHelper.WriteLog(lo_StringBuilder);
104                 return false;
105             }
106             finally
107             {
108                 disAction.Invoke();
109             }
110         }
111 
112     }
View Code
原文地址:https://www.cnblogs.com/aDoc/p/12851190.html