Reprint: http://blog.csdn.net/l345480242/article/details/7588393
Using programmatic transactions
1. What is the difference between getcurrentsession () and opensession ()?
* The session created with Getcurrentsession () is bound to the current thread, while the session created with Opensession () will not
* Sessions created with getcurrentsession () are automatically closed when commit or rollback, and session created with Opensession must be closed manually
2, using getcurrentsession () need to add the following configuration in the Hibernate.cfg.xml file:
* If you are using a local transaction (JDBC Transaction)
<property name= "Hibernate.current_session_contexT_class" >thread</property>
* If you are using a global transaction (JTA Transaction)
<property name= "Hibernate.current_session_context_class" >jta</property>
Using declarative transactions
Declarative transaction Configuration
* Configuration Sessionfactory
* Configure transaction Manager
* Propagation Characteristics of transactions
* Those classes which methods use transactions
2. Writing business logic methods
* Inherit Hibernatedaosupport class, use Hibernatetemplate class persistence, hibernatetemplate
Is the package of hibernate session
* The default rollback is run TimeException (including subclasses of inherited runtimeexception), normal exception not rollback
* When writing business logic methods, it is best to throw exceptions all the way up, in the rendering layer processing (sTRUTs
* Spring transactions need to be set on the business method (transaction boundary defined on the facade class), not added to DAO
3. Understanding the central dissemination of transactions
1. propagation_required: If there is a transaction, the current transaction is supported. If no transaction is turned on.
2. Propagation_supports: If there is a transaction, the current transaction is supported. If there is no transaction, the execution is non-transactional.
3. Propagation_mandatory: If a transaction already exists, the current transaction is supported. Throws an exception if there is no active transaction.
4, Propagation_requires_new: Always open a new transaction. If a transaction exists, the existing transaction is suspended.
5. Propagation_not_supported: Always executes in a non-transactional manner and suspends any existing transactions.
6. Propagation_never: Always executes in a non-transactional manner and throws an exception if there is an active transaction.
7. propagation_nested: If an active transaction exists, it is run in a nested transaction, and if there is no active transaction, the Transactiondefinition.propagation_required property is executed
4. The isolation level of spring transactions
1, Isolation_default: This is a platfromtransactionmanager default isolation level, using the database default transaction isolation level.
2. Isolation_read_uncommitted: This is the lowest isolation level for transactions, which allows another transaction to see the uncommitted data for this transaction.
3, isolation_read_committed: To ensure that a transaction modified data submitted before being read by another transaction. Another transaction cannot read uncommitted data for the transaction.
4, Isolation_repeatalbe_read: This transaction isolation level can prevent dirty read, non-repeatable read. But there may be fantasies to read. In addition to ensuring that one transaction cannot read data that is not committed by another transaction, it ensures that the following conditions are avoided (non-repeatable read).
5. Isolation_serializable This is the most cost-effective, but most reliable, transaction isolation level. Transactions are processed for sequential execution. In addition to prevent dirty reading, do not repeat reading, but also avoid the illusion of reading.
Transaction properties
T1
T2
Required
No
T1
T2
T1
Requirednew
No
T1
T2
T2
Support
No
T1
No
T1
Mandatory
No
T1
Throw exception
T1
Nosupport
No
T1
No
No
Never
No
T1
No
Throw exception
à
<!--configuration Sessionfactory-->
<beanID= "Sessionfacory" class= "ORG.SPRINGFRAMEWORK.ORM. Hibernate3. Localsessionfactorybean ">
<property name= "ConfigloCatIon ">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!--configuration transaction Manager-->
<beanid= "TransactionManager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "Sessionfactory" >
<ref bean= "Sessionfactory"/>
</property>
</bean>
<!--The propagation characteristics of configuration transactions-->
<tx:advice id= "Txadvice" transaction-manager= "TransactionManager" >
<tx:attributes>
<tx:method Name= "ADD* "propagation=" REQUIRED "/>
<tx:method name= "del*" propagation= "REQUIRED"/>
<tx:method name= "modify*" propagation= "REQUIRED"/>
<tx:method name= "*" read-only= "true"/>//read-only improves performance
</tx:attributes>
</tx:advice>
<!--What classes of methods participate in the transaction-->
<aop:config>
<aop:pointCutId= "Allmanagermethod"Expression= "Execution (* com.bjsxt.usermgr.manager.*.* (..))" />
<aop:advisor pointcut-ref= "Allmanagermethod" advice-ref= "Txadvice"/>
</aop:config>
Propagation_required Transaction Management