To ensure data consistency, we often need to introduce the transaction concept during programming. Transactions have four features: atomicity, consistency, isolation, and persistence.
There are two types of transactions: programmatic transactions and clear transactions. A programmatic transaction is to put the transaction processing in the program, while a declarative transaction is to operate through the configuration file or annotation.
There is a declarative transaction concept in spring. through integration with frameworks similar to hibernate, declarative transactions can be well completed.
In fact, no matter how many methods are used to configure hibernate transactions in spring, you cannot escape the following:
1. Configure sessionfactory
2. Configure the transaction container
3. Configure transaction rules
4. Configure the transaction entry
Four Methods for configuring hibernate transactions are provided.
First, you can configure sessionfactory in either of the following ways. cfg. configure sessionfactory in the XML file, and manually configure the data source in the spring configuration file.
Below are two ways to configure sessionfactory (the second method requires two additional packages: commons-DBCP and commons-pool)
<! -- 1. The first method for configuring sessionfactory --> <bean id = "sessionfactory" class = "org. springframework. orm. hibernate3.localsessionfactorybean "> <property name =" configlocation "value =" classpath: hibernate. cfg. XML "/> </bean> <! -- 2. The second method for configuring sessionfactory --> <! -- 2.1 configure the data source --> <bean id = "datasource" class = "org. apache. commons. DBCP. basicdatasource "Destroy-method =" close "> <property name =" driverclassname "value =" com. mySQL. JDBC. driver "> </property> <property name =" url "value =" JDBC: mysql: // localhost: 3306/hibernate_cache "> </property> <property name =" username "value =" root "> </property> <property name =" password "value =" admin "> </property> </bean> <! -- 2.2. Configure sessionfactory --> <bean id = "sessionfactory" class = "org. springframework. orm. hibernate3.localsessionfactorybean "> <property name =" datasource "ref =" datasource "> </property> <property name =" hibernateproperties "> <props> <prop key =" hibernate. hbm2ddl. auto "> Update </prop> </props> </property> <property name =" mappinglocations "> <list> <value> classpath: XML Path corresponding to the object </value> </List> </property> </bean>
So far, Hibernate has successfully handed sessionfactory to spring for management. Now let's take a look at how spring manages hibernate transactions.
The first method is to use the Tx tag to configure transactions.
<! -- Configure the transaction container --> <bean id = "transactionmanager" class = "org. springframework. orm. hibernate3.hibernatetransactionmanager "> <property name =" sessionfactory "ref =" sessionfactory "/> </bean> <! -- Define transaction rules --> <TX: Advice id = "txadvice" transaction-Manager = "transactionmanager"> <TX: Attributes> <TX: method Name = "add *" propagation = "required"/> <TX: method name = "Modify *" propagation = "required"/> <TX: method Name = "del *" propagation = "required"/> <TX: method Name = "*" propagation = "required" Read-Only = "true"/> </TX: Attributes> </TX: Advice> <! -- Define the transaction portal --> <AOP: config> <AOP: pointcut id = "alldaomethod" expression = "execution (* COM. jianxin. dao. *. *(..)) "/> <AOP: Advisor advice-ref =" txadvice "pointcut-ref =" alldaomethod "/> </AOP: config>
Second, use a proxy for configuration.
<! -- Configure the transaction container --> <bean id = "transactionmanager" class = "org. springframework. orm. hibernate3.hibernatetransactionmanager "> <property name =" sessionfactory "ref =" sessionfactory "/> </bean> <! -- Define transaction rules --> <bean id = "transactionproxy" class = "org. springframework. transaction. interceptor. transactionproxyfactorybean "abstract =" true "> <property name =" transactionmanager "ref =" transactionmanager "/> <property name =" transactionattributes "> <props> <! --, Roll back to-, do not roll back to + --> <prop key = "add *"> propagation_required, -exception </prop> <prop key = "Modify *"> propagation_required, + myexception </prop> <prop key = "del *"> propagation_required </prop> <prop key = "*"> readonly </prop> </props> </Property> </bean> <! -- Define the transaction portal --> <bean id = "userdaoproxy" parent = "transactionproxy"> <property name = "target" ref = "userdao"> </property> </bean>
Third, use the interceptor
<! -- Configure the transaction container --> <bean id = "transactionmanager" class = "org. springframework. orm. hibernate3.hibernatetransactionmanager "> <property name =" sessionfactory "ref =" sessionfactory "/> </bean> <! -- Define transaction rules --> <bean id = "transactioninterceptor" class = "org. springframework. transaction. interceptor. transactioninterceptor "> <property name =" transactionmanager "ref =" transactionmanager "/> <property name =" transactionattributes "> <props> <! -- Roll back to-, do not roll back to + --> <prop key = "add *"> propagation_required, -exception </prop> <prop key = "Modify *"> propagation_required, + myexception </prop> <prop key = "del *"> propagation_required </prop> <prop key = "*"> readonly </prop> </props> </Property> </bean> <! -- Define the transaction entry --> <bean id = "proxyfactory" class = "org. springframework. AOP. framework. autoproxy. beannameautoproxycreator "> <property name =" interceptornames "> <list> <value> transactioninterceptor </value> </List> </property> <property name =" beannames "> <list> <value> * Dao </value> </List> </property> </bean>
Fourth, use annotations.
First, write the following statement in the configuration file to enable the annotation function.
<! -- Account opening transaction annotation function --> <TX: annotation-driven transaction-Manager = "transactionmanager"/>
Then, use @ transactional to mark the class or method. If it is marked on the class, all methods in the class will be rolled back. When transactional is defined in the class, it has attributes such as propagation, rollbackfor, and norollbackfor. This attribute is used to define transaction rules, and where is the transaction entry.
The core of the above four methods for configuring hibernate transactions in spring is the same. The difference is only the implementation method. Therefore, you only need to remember four sentences in this blog post to easily understand the core of configuring hibernate transactions in Spring:
1. Configure sessionfactory
2. Configure the transaction container
3. Configure transaction rules
4. Configure the transaction entry