Spring+mybatis Transaction Management

Source: Internet
Author: User

Spring+mybatis Transaction Management

Recently in a project with friends, consider using Springmvc+mybatis to do, before working in the company, for the database This configuration is also someone else, recently because of this project, I went online to learn something about the database configuration, today to share about the spring +mybatis Management affairs.

First of all, there are two things I know about the centralized approach to spring management: The first is programmatic transaction management, the second is declarative transaction management, and there are two types of declarative transaction management, one is configuration and the other is declarative. We generally use declarative transaction management configuration in our work, because this approach does not invade the code. Below I put a configuration to post, is I now and friends to do the project about the configuration of the database.

Java code
  1. <bean id="Dbconfig"
  2. class="Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
  3. <property name="Location" >
  4. <value>classpath:jdbc.properties</value>
  5. </property>
  6. </bean>
  7. <!--configuring data sources-
  8. <bean id="DataSource" class="Org.apache.commons.dbcp.BasicDataSource" >
  9. <property name="driverclassname" value="${jdbc.driver}"/>
  10. <property name="url"
  11. Value="${jdbc.url}"/>
  12. <property name="username" value="${jdbc.username}"/>
  13. <property name="password" value="${jdbc.password}"/>
  14. <property name="initialsize" value="${dbcp.initialsize}"/>
  15. <property name="Minidle" value="${dbcp.minidle}"/>
  16. <property name="Maxidle" value="${dbcp.maxidle}"/>
  17. <property name="maxactive" value="${dbcp.maxactive}"/>
  18. <property name="maxwait" value="${dbcp.maxwait}"/>
  19. <property name="maxopenpreparedstatements" value="${dbcp.maxopenpreparedstatements}"/>
  20. </bean>
  21. <bean id="sqlsessionfactory" class="Org.mybatis.spring.SqlSessionFactoryBean" >
  22. <property name="mapperlocations" value="Classpath:com/wealth/map/*.xml"/>
  23. <property name="DataSource" ref="DataSource"/>
  24. </bean>
  25. <bean class="Org.mybatis.spring.mapper.MapperScannerConfigurer" >
  26. <property name="basepackage" value="Com.wealth.dao"/>
  27. <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"/>
  28. </bean>
  29. <!--configuration Transaction Manager-
  30. <bean id="TransactionManager"
  31. class="Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
  32. <property name="DataSource" ref="DataSource"/>
  33. </bean>
  34. <!--start the spring transaction annotations, and the transaction annotations are done here--
  35. <tx:annotation-driven transaction-manager="TransactionManager"/>



Today our focus is not on these configurations, but on the transaction characteristics:
1, the isolation level of the transaction
Isolation level refers to the degree of isolation between several concurrent transactions. Five constants representing the isolation level are defined in the Transactiondefinition interface:
Transactiondefinition.isolation_default: This is the default value that represents the default isolation level for using the underlying database. For most databases, this value is usually transactiondefinition.isolation_read_committed.
Transactiondefinition.isolation_read_uncommitted: This isolation level indicates that one transaction can read data that has been modified by another transaction but has not yet been committed. This level does not prevent dirty reads, non-repeatable reads, and Phantom reads, so the isolation level is rarely used. For example, PostgreSQL does not actually have this level.
Transactiondefinition.isolation_read_committed: This isolation level indicates that a transaction can only read data that has been committed by another transaction. This level prevents dirty reads, which is the recommended value in most cases.
Transactiondefinition.isolation_repeatable_read: This isolation level indicates that a transaction can repeatedly execute a query multiple times throughout the process, and that the records returned are the same each time. This level protects against dirty reads and non-repeatable reads.
Transactiondefinition.isolation_serializable: All transactions are executed one after the other, so that there is absolutely no possibility of interference between transactions, that is, the level prevents dirty reads, non-repeatable reads, and Phantom reads. However, this will severely affect the performance of the program. This level is not normally used.
2. Communication behavior of the affairs

The so-called transaction propagation behavior is that if a transaction context already exists before the current transaction is started, there are several options to specify the execution behavior of a transactional method.

In fact, I think the propagation behavior of a transaction is one of the propagation behavior of the method in which you perform another transaction in the method of the current transaction.

propagation_required--supports the current transaction and creates a new transaction if there is no current transaction. This is the most common choice.
propagation_supports--supports the current transaction and is executed in a non-transactional manner if no transaction is currently in use.
The propagation_mandatory--supports the current transaction and throws an exception if there is no current transaction.
propagation_requires_new--a new transaction, suspending the current transaction if a transaction is currently present.
The propagation_not_supported--executes the operation in a non-transactional manner, suspending the current transaction if a transaction is currently present.
The propagation_never--is executed in a non-transactional manner and throws an exception if a transaction is currently present. transactiondefinition.propagation_nested--If a transaction is currently present, create a transaction to run as a nested transaction for the current transaction, or if there is no current transaction, The value is equivalent to transactiondefinition.propagation_required.

Spring+mybatis Transaction Management

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.