I have recorded that the mvc framework has integrated database transaction processing. It is easy to enable the transaction, as long as you add the [DbTransaction] annotation (attribute) on the action, for example:
[HttpPost, DbTransaction]
Public void Create (){
}
The processing method of this annotation is that the database will automatically roll back as long as any exception occurs in the action.
The wojilu system uses a large number of comments [feature, I prefer this word, egg students like to annotate this word] to control Method, such as Action permission verification, security Verification is implemented through annotation and reflection. Many methods can be obtained through obtaining annotations.
Specific implementation methods:
1 /// <summary>
2 // enable database transactions on the current action and support multi-database transactions
3 /// </summary>
4 [Serializable, AttributeUsage (AttributeTargets. Method)]
5 public class DbTransactionAttribute: Attribute, IActionFilter {
6
7 private static readonly ILog logger = LogManager. GetLogger (typeof (DbTransactionAttribute ));
8
9 public void BeforeAction (ControllerBase controller ){
10
11 DbContext. beginAndMarkTransactionAll ();
12}
13
14 public void AfterAction (ControllerBase controller ){
15
16 if (controller. ctx. utils. getException () = null ){
17 DbContext. commitAll ();
18}
19 else {
20 try {
21 DbContext. rollbackAll ();
22}
23 catch (Exception ex ){
24 logger. Error ("data operation (rollbackAll):" + ex. StackTrace );
25 throw ex;
26}
27 finally {
28 logger. Info ("DbTransaction: rollbackAll ");
29 DbContext. closeConnectionAll ();
30}
31}
32
33}
34
35 public int Order {get; set ;}
36
37}
Ii. Manual use of transactions
// Start the transaction
DbContext. beginTransactionAll ();
// Submit
DbContext. commitAll ();
// Rollback
Try {
DbContext. rollbackAll ();
}
Catch (Exception ex ){
// Handle exceptions
}
Finally {
// Close the database connection
DbContext. closeConnectionAll ();
}
To explain, why is beginTransactionAll, instead of beginTransaction? Wojilu ORM supports multiple databases. Maybe two tables are involved in one data insertion, but these two tables are distributed in two different databases. In this case, wojilu ORM opens two connections, when you use the beginTransactionAll method, transactions are enabled for these two connections at the same time. During the commitAll process, if any exception occurs, rollbackAll will roll back the two database operations. At last, closeConnectionAll will also close the connection between the two databases.
That is to say, wojilu naturally supports distributed database transactions.
3. Manually manage transactions for specific "connection"
If you need to manually write SQL statements or make it clear that your transactions are only for specific databases, you can switch out the methods provided by wojilu ORM and adopt the original. net database operation method.
1) first obtain the current database connection
IDbConnection connection = DbContext. getConnection (type );
2) Enable transaction management on the connection. The subsequent process is the same as that of conventional databases, for example
Connection. BeginTransaction ();
// More code...
Reference: Reading Extension
Introduction to the ORM of wojilu System
Do while (time ++ ){
Love ++
}