当前位置:网站首页>A usage example that can be compatible with various database transactions

A usage example that can be compatible with various database transactions

2022-07-05 10:38:00 Full stack programmer webmaster

Incoming entity execution ( Additable modify Delete ) Business .

                    IDbHelper dbHelper = new OracleHelper(ConfigHelper.GetConfigString("BusinessDbConnection"));
                    bool result = true;
                    try
                    {
                         dbHelper.BeginTransaction();
              // Main table 
                         TE_AREAManager manager = new TE_AREAManager(dbHelper, userInfo);
                         TE_AREAEntity tE_AREAEntity = manager.GetObject(dbHelper.SqlSafe(eatxtAREA_ID));
                         manager.Delete(tE_AREAEntity);
             // Sub table 
                         TE_AREA_SUBManager submanager = new TE_AREA_SUBManager(dbHelper, userInfo);
                         TE_AREA_SUBEntity tE_AREA_SUBEntity = submanager.GetObject(dbHelper.SqlSafe(eatxtAREA_ID));
                         submanager.Delete(tE_AREA_SUBEntity);
             // Transaction submission  
                         dbHelper.CommitTransaction();
                    }
                    catch(Exception ex)
                    {
             // Transaction rollback 
                        dbHelper.RollbackTransaction();
                        result=false;
                    }

You can pass in sql Statements execute transactions

IDbHelper dbHelper = new OracleHelper(ConfigHelper.GetConfigString("BusinessDbConnection"));
                    bool result = true;
                    try
                    {
                         dbHelper.BeginTransaction();
                         string   commandText = " DELETE FROM TE_AREA WHERE ID=" + dbHelper.SqlSafe(id);
                         dbHelper.ExecuteNonQuery(sqlString);

                         commandText = " DELETE FROM TE_AREA_SUB WHERE ID=" + dbHelper.SqlSafe(id);
                         dbHelper.ExecuteNonQuery(commandText);

                         dbHelper.CommitTransaction();
                    }
                    catch(Exception ex)
                    {
                        dbHelper.RollbackTransaction();
                        result=false;
                    }

You can also pass in entities at the same time ,SQL sentence

 IDbHelper dbHelper = new OracleHelper(ConfigHelper.GetConfigString("BusinessDbConnection"));
                    bool result = true;
                    try
                    {
                         dbHelper.BeginTransaction();
               // Main table 
                         TE_AREAManager manager = new TE_AREAManager(dbHelper, userInfo);
                         TE_AREAEntity tE_AREAEntity = manager.GetObject(dbHelper.SqlSafe(eatxtAREA_ID));
                         manager.Delete(tE_AREAEntity);
               // Sub table 
                         TE_AREA_SUBManager submanager = new TE_AREA_SUBManager(dbHelper, userInfo);
                         TE_AREA_SUBEntity tE_AREA_SUBEntity = submanager.GetObject(dbHelper.SqlSafe(eatxtAREA_ID));
                         submanager.Delete(tE_AREA_SUBEntity);
               
               // perform SQL sentence 
               string commandText = " DELETE FROM JINTIANDA WHERE ID=" + dbHelper.SqlSafe(id);
               dbHelper.ExecuteNonQuery(commandText);

               // Transaction submission  
                         dbHelper.CommitTransaction();
                    }
                    catch(Exception ex)
                    {
              // Transaction rollback 
                        dbHelper.RollbackTransaction();
                        result=false;
                    }

The above transaction processing method is based on the underlying code of Jiri Gala general permission management component ,dbHelper, Compatible with all kinds of data Library transactions , It's very easy .

Welcome to provide your own experience , Jointly improve development efficiency .

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/109843.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051012000794.html