When spring declares a transaction, what will happen if there is a commit in the code?

Source: Internet
Author: User

When using spring-declared transactions and spring-provided templates for the persistent layer to operate databases, in principle, do not write transaction control statements (COMMIT) in the code ).
1. When jdbctemplate and JDBC are used for integration:

public void testInsert(int id, String val) {this.jdbcTemplate.update("insert into A (ID, VAL) values (?, ?)", id, val);try {jdbcTemplate.getDataSource().getConnection().commit();System.out.println("  jdbcTemplate.getDataSource().getConnection().commit()");} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

The commit statement has no effect on the testinsert method. During the transaction management declared by spring, commit does not affect spring transactions.
Whether the classes with the testinsert method are managed by spring or created in the new way, jdbctemplate. getdatasource (). getconnection (). commit () does not affect transactions. the reason is that when there is no transaction declaration,
The resulting connect is automatically submitted in jdbctemplate. getdatasource (). getconnection (). commit (); has been automatically submitted before, and when there is a transaction declaration ,. commit () does not work.
2. Integrate hibernate with hibernatetemplate:
When you configure <property name = "current_session_context_class"> thread </property> of hibernate, the management transaction must be displayed in the code.
When a transaction is managed by spring, <property name = "current_session_context_class"> thread </property> cannot be configured, and cannot be submitted in the code.
3. Integrate mybatis:
A. When mybatis is not integrated with spring, it must be submitted by sqlsession. Commit () to save the data.
B. when integrating with spring: If transaction management is not declared, each database operation is a transaction.
When the sqlsession is composed of Org. mybatis. spring. sqlsessiontemplate cannot be submitted in the code. when sqlsession is created by sqlsessionfactory. when opensession () is obtained, submission is not affected. (Each database operation is independent)

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");MybatiscustomerMapper mapper = ctx.getBean("mybatiscustomerMapper", MybatiscustomerMapper.class);Mybatiscustomer customer = new Mybatiscustomer();customer.setId(new BigDecimal(1));customer.setName("name3");mapper.insert(customer); //##this will succeedString nullStr = null;nullStr.length();customer.setId(new BigDecimal(2));mapper.insert(customer);//##this will NOT succeed

C. When a transaction statement is managed, sqlsession must be obtained by org. mybatis. Spring. sqlsessiontemplate, and cannot be submitted through sqlsession. Commit () in the code.

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.