Spring JDBC transaction propagation feature

Source: Internet
Author: User

Integrated Development of Spring and JDBC: (2)

Exception Handling through JDBC template class

Propagation Features of Transaction Processing

Transaction isolation

@ Transactional (noRollbackFor = RuntimeException. class)
Public void save (User user) throws Exception {
// TODO Auto-generated method stub
This. template. update ("insert into user values (?,?) ",
New Object [] {new Integer (user. getId (), user. getName ()},
New int [] {java. SQL. Types. INTEGER, java. SQL. Types. VARCHAR });
System. out. println ("inserted successfully ....");
Throw new RuntimeException ();
}
 


We have set the parameter: noRollbackFor, so even if a runtime exception (nocheck exception) is encountered, it will not be rolled back.

By default, Spring only rolls back when an exception occurs during runtime. However, we can consider the classes that need to be rolled back.

@ Transactional (rollbackFor = Exception. class)
Public void save (User user) throws Exception {
// TODO Auto-generated method stub
This. template. update ("insert into user values (?,?) ",
New Object [] {new Integer (user. getId (), user. getName ()},
New int [] {java. SQL. Types. INTEGER, java. SQL. Types. VARCHAR });
System. out. println ("inserted successfully ....");
Throw new Exception ();
}


At this time, we specify that the Exception will be rolled back, so the execution result is transaction rollback.

The following is the first example of the transaction propagation feature: Required nerver provides the Code:

@ Transactional (propagation = Propagation. REQUIRED)
Public void save (User user ){
// TODO Auto-generated method stub
This. template. update ("insert into user values (?,?) ",
New Object [] {new Integer (user. getId (), user. getName ()},
New int [] {java. SQL. Types. INTEGER, java. SQL. Types. VARCHAR });
System. out. println ("inserted successfully ....");
Userdao_1.save (null );
}


Code 2:

@ Transactional (propagation = Propagation. NEVER)
Public void save (User user ){
// TODO Auto-generated method stub
This. getTemplate (). update ("insert into user values (21, fasd )");
System. out. println ("insert successful ....??????????????? ///");
}


Code 1 requires a transaction. When the dao layer is executed, the transaction is allocated because no transaction is detected. However, a method is nested, and the method in Code 2 does not require PROPAGATION. NEVER runs in the transaction environment, so an exception is thrown.

Because Code 2: An exception occurs when running in the transaction environment of code 1 (this exception is a subclass of RuntimeExcpetion). Therefore, all data rollback is not inserted;

Force: Code 1:

@ Transactional (propagation = Propagation. NEVER)
Public void save (User user ){
// TODO Auto-generated method stub
This. template. update ("insert into user values (?,?) ",
New Object [] {new Integer (user. getId (), user. getName ()},
New int [] {java. SQL. Types. INTEGER, java. SQL. Types. VARCHAR });
System. out. println ("inserted successfully ....");
Userdao_1.save (null );
}


Code 2:

@ Transactional (propagation = Propagation. MANDATORY)
Public void save (User user ){
// TODO Auto-generated method stub
This. getTemplate (). update ("insert into user values (21, fasd )");
System. out. println ("insert successful ....??????????????? ///");
}


The running result throws an exception: but the code modifies the database without having to finish running the SQL statement of the transaction. However, code 2 requires transactions but runs in an environment without transactions, so it runs out of an exception.

Supported:

@ Test
Public void save () throws Exception {
BeanFactory bf = new ClassPathXmlApplicationContext ("applicationContext. xml ");
UserDAO dao = (UserDAO) bf. getBean ("userdao ");
User user = new User ();
User. setId (11 );
User. setName ("Liu Qiang ");
Dao. save (user );

}


Code 1:

@ Transactional (propagation = Propagation. NEVER)
Public void save (User user ){
// TODO Auto-generated method stub
This. template. update ("insert into user values (?,?) ",
New Object [] {new Integer (user. getId (), user. getName ()},
New int [] {java. SQL. Types. INTEGER, java. SQL. Types. VARCHAR });
System. out. println ("inserted successfully ....");
Userdao_1.save (null );
}
 


Code 2:

@ Transactional (propagation = Propagation. SUPPORTS)
Public void save (User user ){
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.