We can focus on the author's account, focus on learning from scratch notes Spring Collection. We can also learn from a catalog visit the author's blog garden blog. The film documents the learning and the sharing of information on employment-based programmers dark horse class video, and record notes and their own views. Welcome to learn and discuss.
[Notes] Spring Spring learn from scratch learning route
What matters
Transaction: a set of logical operations on the composition of the respective units of the set operation, either all succeed or all fail.
Characteristics of the transaction
Atomic: A transaction indivisible
consistency: executed before and after the transaction data integrity consistent
Isolation: execute a transaction should not be subject to interference from other transactions
persistence: Once the end of the transaction, the data is persisted to the database
If you do not consider the isolation caused by security problems
- Reading problems
Dirty read: A transaction reads data from another uncommitted transaction
can not repeatable read: A transaction reads data update of another transaction has been submitted, resulting in a transaction multiple query results to differ
phantom reads, phantom reads: a transaction data insert read another transaction has been submitted, resulting in a transaction multiple queries inconsistent results. - Write problem
of lost updates
Reading solve the problem
Set the transaction isolation level
- Read uncommitted: read uncommitted, read any problem can not be resolved. (Highest efficiency, but least secure)
the Read committed: Read Committed to address the dirty read, but non-repeatable reads and phantom reads can occur.
Repeatable read: Repeatable read, solve dirty reads and non-repeatable reads, but phantom reads can occur.
Serializable: Reading solve all the problems.
Spring's transaction management API
PlatformTransactionManager: Platform Transaction Manager
Platform Transaction Manager: Interface, is the real target for the Spring-managed transactions.
- DataSourceTransactionManager: the underlying transaction management using JDBC
HibernateTransactionManager: the underlying transaction management using Hibernate
TransactionDefinition: transaction definition information
Transaction Definition: related information used to define transaction isolation level, timeout information, communication behavior, whether or not read-only
TransactionStatus: state of affairs
The state of affairs: the state of affairs for the object recorded in the transaction management process.
: Relationship Management API transaction
when the transaction management Spring First Platform Transaction Manager The transaction definition information management transaction, the transaction manager process, resulting in various states, these states recording information to the transaction state objects in.
Spring propagation behavior
In the actual development, in particular, you may encounter complex business logic layer, which numerous affairs, may also call each other. This time we need to carry out a transaction propagation behavior management.
Spring provides seven transaction propagation behavior:
to ensure a plurality of operations in the same transaction
$ \ color {red} {** } $ PROPAGATION_REQUIRED: default value, if there are A transaction using the transaction A, the operation It included. If A does not create a new transaction, the operation will include it
PROPAGATION_SUPPORTS: support services, if A has a transaction, use the A transactions. A If there is no transaction, the transaction is not used.
PROPAGATION_MANDATORY: A If there is a transaction, use the A transactions. A If there is no transaction, throw an exception.
Ensure multiple operations in a transaction not in the same
$ \ color {red} {** } $ PROPAGATION_REQUIRES_NEW: A If there is a transaction, the transaction A hang (pause), create a new transaction, containing only its own operations. If A is not a transaction, create a new transaction, including its own operations.
PROPAGATION_NOT_SUPPORTED: A If there is a transaction, the transaction A pending. Do not use transaction management.
PROPAGATION_NEVER: A If there is a transaction, reported abnormal.
Nested transaction
$ \ color {red} {** } $ PROPAGATION_NESTED: nested transaction, if the transaction A, A transaction is performed in accordance with, the execution is completed, setting a savepoint, an operation B, if there is no abnormality, is performed by, if abnormal, can select the most rollback to the initial position, it can be rolled back to the savepoint.
Spring's transaction management built environment
Step 1: Create Senvice of interface and implementation class
package com.tyust.tx.demo1;
public interface AccountService {
public void transfer(String form, String to ,Double money);
}
package com.tyust.tx.demo1;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
@Override
/**
* from:传出账户
* to:转入账户
* money:金额
*/
public void transfer(String form, String to, Double money) {
accountDao.outMoney(form, money);
accountDao.inMoney(to, money);
}
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
}
Step 2: Create a DAO interface and implementation class
package com.tyust.tx.demo1;
import org.springframework.jdbc.core.JdbcTemplate;
public interface AccountDao {
public void outMoney(String from, double money);
public void inMoney(String to, double money);
}
package com.tyust.tx.demo1;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public void outMoney(String from, double money) {
this.getJdbcTemplate().update("update account set money = money - ? where name = ?", money, from);
}
@Override
public void inMoney(String to, double money) {
this.getJdbcTemplate().update("update account set money = money + ? where name = ?", money, to);
}
}
Step Three: Configure Senvice and DAO: Spring to manage
<bean id = "accountDao" class="com.tyust.tx.demo1.AccountDaoImpl">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id = "accountService" class="com.tyust.tx.demo1.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
Step Four: Configure JDBC connection pool and templates
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3p0连接池 -->
<bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"></property>
</bean>
Step five: Test
package com.tyust.tx.demo1;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:tx.xml")
public class SpringDemo {
@Resource(name = "accountService")
private AccountService accountService;
@Test
public void demo() {
accountService.transfer("关羽", "张飞", 1000d);
}
}
Output
Why introduction Affairs
The case is no transaction implementation method, if the transit transfer () method and an abnormal transfer between, there will turn out to achieve, and transfer did not materialize, which in real life is not allowed. So it is necessary to protect the transaction
Spring's transaction management - programmatic transaction (need to manually write code)
Step 1: Configure Platform Transaction Manager
<!-- 配置事务平台管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
Step Two: Spring provides a template class transaction management
<bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
The third step: template transaction management in the business layer injection
//注入事务管理的模板
private TransactionTemplate transactionTemplate;
</bean>
<bean id="accountService"
class="com.tyust.tx.demo1.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
<property name="transactionTemplate"
ref="transactionTemplate"></property>
</bean>
The fourth step: write transaction management code
public void transfer(final String form,final String to, final Double money) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
accountDao.outMoney(form, money);
int i =1/0;
accountDao.inMoney(to, money);
}
});
}
Step five: Test
findings do not appear to achieve the roll-out, but did not realize the situation into
Spring's transaction management - declarative transaction management (by configuring) --- AOP
XML declarative transaction management approach
The first step: introducing aop Development Kit
Step two: Transfer to restore the environment
to add on an example of a transaction code editor to delete all
Step Three: Configure the transaction manager
<bean id ="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
Step Four: Configure Enhanced
<!-- 配置事务的增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
事务管理规范
<tx:method name="save" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update" propagation="REQUIRED" />
<tx:method name="delete" propagation="REQUIRED" />
<tx:method name="find" read-only="true"/>
read-only: read-only, additions and deletions not only check
timeout: -1 -> been effective
Step five: AOP configuration
<aop:config>
<aop:pointcut expression="execution(* com.tyust.tx.demo2.AccountServiceImpl.*(..) )" id="pointcut1"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
</aop:config>
Step Six: test
findings do not appear to achieve the roll-out, but did not realize the situation into
Declarative transaction management approach notes
The first step: Development Kit introduced aop of
Step two: Transfer to restore the environment
Step Three: Configure the transaction manager
first three steps as before
Step 4: Enable notes Affairs
<tx:annotation-driven transaction-manager="transactionManager"/>
Step five: add annotations in the business layer
Only need to AccountServiceImpl
type the above plus @Transaction
notes to
the sixth step: test
findings do not appear to achieve the roll-out, but did not realize the situation into