1. Spring declarative transactions
1.1 Spring's transaction manager
Spring does not directly manage transactions, but delegates the responsibility for managing transactions to transaction implementations of a specific platform provided by JTA or corresponding persistence mechanisms. The spring container is responsible for the operation of things, the spring container acts as a face, and the transaction method is called enhancement processing. The generated proxy object method is the target method + enhancement that is, the transaction programmer only uses the crud operation, that is, the target method and the declared method should run in the transaction.
Spring provides many built-in transaction manager implementations:
DataSourceTransactionManager: Located in the org.springframework.jdbc.datasource package, the data source transaction manager provides a single javax.sql.DataSource transaction management, for transaction management of Spring JDBC abstract framework, iBATIS or MyBatis framework;
JdoTransactionManager: is located in the org.springframework.orm.jdo package, providing transaction management of a single javax.jdo.PersistenceManagerFactory, for transaction management when integrating the JDO framework;
JpaTransactionManager: is located in the org.springframework.orm.jpa package, providing support for a single javax.persistence.EntityManagerFactory transactions, and is used to manage transactions when integrating JPA implementation framework;
HibernateTransactionManager: is located in the org.springframework.orm.hibernate3 package, providing support for a single org.hibernate.SessionFactory transaction, used for transaction management when integrating the Hibernate framework; this transaction manager only supports Hibernate3+ version, and Spring3.0+ version only supports Hibernate 3.2+ version;
JtaTransactionManager: is located in the org.springframework.transaction.jta package, providing support for distributed transaction management, and delegating transaction management to the Java EE application server transaction manager;
OC4JjtaTransactionManager: Located in the org.springframework.transaction.jta package, Spring provides an adapter to OC4J10.1.3+ application server transaction manager. This adapter is used to support advanced transactions provided by the application server;
WebSphereUowTransactionManager: Located in the org.springframework.transaction.jta package, Spring provides an adapter for WebSphere 6.0+ application server transaction manager. This adapter is used to support advanced transactions provided by application servers;
WebLogicJtaTransactionManager: Located in the org.springframework.transaction.jta package, Spring provides an adapter to WebLogic 8.1+ Application Server Transaction Manager. This adapter is used to support advanced transactions provided by the application server.
Spring not only provides these transaction managers, but also provides managers for JMS transaction management, etc. Spring provides a consistent transaction abstraction as shown in the figure.
spring and hibernate
illustrate:
Spring does some preparations before calling the specific transaction manager, setting the transaction's read and write policies in advance, and these transaction policies are public things and are written in the spring configuration file. The processing of these contents needs to be placed in an abstract class.
2. Transaction processing in the integration of spring and hibernate
1.2 Introducing properties configuration file in xml form
<property name="locations"> <value>classpath:jdbc.properties</value> </property>
Configure dbcp data source
<bean id="dataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>Introduce sessionfactory, use hibernate external configuration file
<bean id="sessionFactory2"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean>
Inject dao and service layers
<bean id="personDao"> <property name="sessionFactory"> <ref bean="sessionFactory2"/> </property> </bean> <bean id="personService"> <property name="personDao"> <ref bean="personDao"/> </property> </bean>
Configure the hibernate transaction manager
<bean id="transactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory2"/> </property> </bean>
Configure declarative transactions
effect:
1. Tell spring container transaction manager
2. Tell spring container what method to use what transaction
<tx:advice transaction-manager="transactionManager" id="tx"> <tx:attributes> <!-- name Scope of target method islation isolation level propagation propagation propagation attribute read-only true read-only transaction false read-write transaction--> <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/> </tx:attributes> </tx:advice>
Configure entry point
<aop:config> <aop:pointcut expression="execution(* cn.qjc.hibernate.service.impl.*.*(..))" id="perform"/> <span style="white-space:pre"> </span><!-- Apply pointcut to enhancement method--> <aop:advisor advice-ref="tx" pointcut-ref="perform"/> </aop:config>
dao implementation class
* Implementation method 1: Inherit HibernateDaoSupport * @author qjc */ public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao{ @Override public void savePerson(Person person) { this.getHibernateTemplate().save(person); } }test
...
Notice:
1. If a DAO class inherits HibernateDaoSupport, you only need to inject SessionFactory into the spring configuration file.
2. If a DAO class does not inherit HibernateDaoSupport, it needs to have a SessionFactory attribute and inject it into the configuration file.
<bean id="hibernateTemplate"> <property name="sessionFactory" ref="sessionFactory2"></property> </bean>
1.2 In the form of annotation
1. Apply spring's automatic scanning mechanism in configuration files
<context:component-scan base-package="cn.qjc"/>
2. Introduce annotation parser in the configuration file
<tx:annotation-driven transaction-manager="transactionManager"/>
3. Annotate via @Transaction at the service layer
Note: If annotated as a read-only transaction at the class level, the transaction settings of @Transaction annotated in the methods in this class will take precedence over the transaction settings of the class level annotated.
1.3 Propagation properties of spring transactions
required: The business method needs to be run in a transaction. If the method is already in a transaction when it is running, then join it to the transaction, otherwise create a new transaction for yourself (default)
not-supported:spring container does not open a transaction. If the method is called in a transaction, the transaction will be suspended. After the method is finished, the transaction will be restored.
requiresnew: Whether or not a transaction exists, the business method will always create a new transaction.
mandatorky: A business method can only be executed in an existing transaction. If the business method is called without a transaction, the container throws an exception.
In addition, there are properties such as supports, never, nested, etc., but the default is usually used
propagation="required" This configuration can solve the transaction nesting problem. What is transaction nesting?
for example:
There are transactions in the workflow framework and a certain method that operates the service layer. The service layer also has its own transactions. When the service is executed, transaction nesting will occur, that is, the method itself has transactions, and the methods in the method also have transactions. This is transaction nesting. Spring solves this problem through the transaction propagation attribute propagation="required".
1.4 OpenInSessionView
After s2sh integration, spring manages transactions. Since it uses spring's declarative transaction processing method, the session is closed immediately after calling this.getHibernateTemplate() method. If there are transactions in the currently executed method, the session is closed when the method of the transaction environment is called. So an exception will be generated when the value is output on the page.
The processing method is: OpenSessionInview mode (configured in web.xml)
<filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <!-- singleSession is true by default. If set to false, it is equal to no OpenSessionInView --> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/PersonService</url-pattern> </filter-mapping>
From the above code, we can see that when submitting a request, the session has been turned on in the OpenSessionInView, and the session must be closed after the response, which means that the openSessionInView must be before the filter of struts2. (Put the struts2 filter position above)
However, it also has disadvantages to enable OpenSessionInView: because the session shutdown is delayed, and the first-level cache of hibernate is in the session, which will cause a large amount of cached data to stay in memory for a long time.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.