spring transaction
1 Understand Transaction
- Introduce Spring's transaction manager
a JDBC transactions
b Hibernate transactions
2 Programing transaction in Spring
One approach to adding transaction to your code is to programmly add transactional boundary using transiationTemplate class.
Programming is good when you want complete control over transactional boundary.but you have to use spring specific class.In most case ,your tansactional needs will not require such precise control over transactional boundaries.That is why you will typically choolse to declare transaction support
public void enrollStudentInCourse() {
transactionTemplate.execute(
new TransactionCallback() {
public Object doInTransaction(TransactionStatus ts) {
try {
// do stuff Runs within doInTransaction()
} catch (Exception e) {
ts.setRollbackOnly(); //Calls setRollbackOnly() to roll Calls setRollbackOnly() //to roll back
}
return null; //If successful, transaction is committed
}
}
);
}
3 Declaring transactions
Spring's support for declarative transaction management is implementedd through Spirng's AOP framework.
com.springinaction.training.service.CourseService
//Bean being proxied //Transaction manager //Transaction attribute source - Understanding transaction attributes
In Spring transaction attribute is a description of how transaction policies should be
applied to a methods
a Propagation behavior
Propagation behavior What it means
PROPAGATION_MANDATORY indicate that the method must run within a transaction.If no transaction is in progress
an exception will be thrown
PROPAGATION_NESTED
PROPAGATION_NEVER indicate that the method can not run withi a transaction. if a transaction exist an exception will be thrown.
PROPAGATIOM_NOT_SUPPORT Indicates that the method should not run within a transaction. If an existing transaction is in progress, it will be suspended for the
duration of the method.
PROPAGATION_REQUIRED indicate that the current method must run within a transaction.if an existing transaction is in progress,the ,method will run with the transaction
otherwise a new transaction will be started
PROPAGATION_REQUIRENEW indicates that the current must run within its own
transaction.A new transaction is started and an existing transaction will be suspend
PROPAGATION_SUPPORT indicate the current mehtod does not require a transaction.but may run if on is already in progress
b Isolation levels
Isolation level What it means ISOLATION_DEFAULT Using the defaul isolation level of the underlying database ISOLATION_READ_UNCOMMITTED Allows you read change that have not yet been commit May result in dirty read,phantom read,nonrepeatable read ISOLATION_READ_COMMITTED Allows reads from concurrent transactions that have bean committed.Dirty read are prevent.but platform and norepeatable reads may still occur. ISOLATIOM_REPEATABLE_READ Multiple read the same field will yield the same result ,unless changed by the transaction itself.Dirty reads ,nonrepeatable are all prevented phantom may still occur ISOLATION_SERIALIZABLE This fully ACID-compliant isolation level ensusme that dirty read,unrepeatable read ,phantom read are all prevented.And this is the most slowest isolation since it is typically accomplished by doing full table lock on the tables in the transaction.
c Read-Only If a transaction performs only read operation against the underlying datastore.when a transaction begin ,it only make sense to declare a transaction as read only on mehtods with propagation behavior which start a new transaction. Furthermore ,if you are Hibernate as persistence mechanism,declaring a transaction as read only will reult in Hibernate flush mode being set to FLUST_NEVER.this tell hibernate to avoid synchroniztion of objects with database. d Transaction timeout Suppose that your transaction becomes unexpectedly long-running transaction.Because transaction may invole locks on the underlying database.Instead of waiting it out ,you can delcare a transaction to automaitically roll back. because timeout clock begin ticking when a transaction start. it only make sense to declare a transaction timeout on methods with propagation behavior that start a new transaction. 2) Declaring a simple transaction policy PROPAGATION_REQUIRES_NEW ISOLATION_REPEATABLE_READ 4 Declaring transactions by method name
- Using NameMatchTransactionAttributeSource The properties property of NameMatchTransactionAttributeSource maps mehtod to a transaction property descriptor. the property descriptor takes the following form: Propagation,isolation,readOnly,-Exception,+Exception
PROPAGATION_REQUIRES_NEW 2) Specifying the transaction Isolation level PROPAGATION_REQUIRES_NEW,ISOLATION_REPEATABLE_READ 3) Using real-only transaction PROPAGATION_REQUIRED,ISOLATION_REPEATABLE_READ,readOnly 4)Specifying rollback rules You can sepcify that a transaction be rollback on specify checked exception PROPAGATION_REQUIRES_NEW,ISOLATION_REPEATABLE_READ, -CourseException Exception can be marked as negative(-) or postive(+) Negative exception will trigger the roll back if the exception (or sublclass of it) is thrown.Postive exception on the other hand indicate that the transacton should be commit even if the exception is thrown 5)Using wildcard matches PROPAGATION_SUPPORTS 6 Short-cut name match transaction PROPAGATION_REQUIRES_NEW