Configuration of four declarative transactions of spring-hibernate transactions
Keywords: configuration of four declarative transactions of spring-hibernate transactionsConfiguration of four types of declarative transactions in spring-the configuration of two beans under hibernate transaction is used below.
<! -- Define the Transaction Manager (declarative transaction) -->
<Bean id = "transactionmanager"
Class = "org. springframework. Orm. hibernate3.hibernatetransactionmanager">
<Property name = "sessionfactory">
<Ref local = "sessionfactory"/>
</Property>
</Bean>
<! -- The business logic layer (positive encapsulation of each DAO layer) is mainly used <facade mode> -->
<Bean id = "fundservice"
Class = "com. Jack. Fund. Service. serviceimpl. fundservice">
<Property name = "operdao">
<Ref bean = "operatordao"/>
</Property>
<Property name = "producedao">
<Ref bean = "fundproducedao"/>
</Property>
<Property name = "customerdao">
<Ref bean = "customerdao"/>
</Property>
<Property name = "accountdao">
<Ref bean = "accountdao"/>
</Property>
<Property name = "fundaccountdao">
<Ref bean = "fundaccountdao"/>
</Property>
<Property name = "fundtransdao">
<Ref bean = "fundtransdao"/>
</Property>
</Bean>
There may be many other modules. <Bean id = "fundservice"/> is only a module.
The first method is as follows. It is also our most common method. It applies when your database tables are small.
<Bean id = "fundservicedaoproxy"
Class = "org. springframework. transaction. Interceptor. transactionproxyfactorybean">
<! -- Configure the Transaction Manager -->
<Property name = "transactionmanager">
<Ref bean = "transactionmanager"/>
</Property>
<! -- This attribute specifies whether the target class is a proxy object in this province. If the target class does not implement any classes, set it to true to represent itself. -->
<Property name = "proxytargetclass">
<Value> false </value>
</Property>
<Property name = "proxyinterfaces">
<Value> com. Jack. Fund. Service. ifundservice </value>
</Property>
<! -- Target Bean -->
<Property name = "target">
<Ref bean = "fundservice"/>
</Property>
<! -- Configure transaction properties -->
<Property name = "transactionattributes">
<Props>
<Prop key = "delete *"> propagation_required </prop>
<Prop key = "add *"> propagation_required </prop>
<Prop key = "Update *"> propagation_required </prop>
<Prop key = "Save *"> propagation_required </prop>
<Prop key = "find *"> propagation_required, readonly </prop>
</Props>
</Property>
</Bean>
There may be other xxxservicedaoproxy. You can see that a service proxy service is configured for each function module. If the number of modules is too large, the Code may seem a little too much, and they are just a little different. At this time, we should think of the idea of inheritance. Use the second method.
Method 2: Configure declarative transactions as follows. This situation is suitable for a relatively large number of modules.
<! -- Simplify the configuration with the idea of inheritance. abstract = "true" -->
<Bean id = "transactionbase"
Class = "org. springframework. transaction. Interceptor. transactionproxyfactorybean"
Lazy-init = "true" abstract = "true">
<! -- Configure the Transaction Manager -->
<Property name = "transactionmanager">
<Ref bean = "transactionmanager"/>
</Property>
<! -- Configure transaction properties -->
<Property name = "transactionattributes">
<Props>
<Prop key = "delete *"> propagation_required </prop>
<Prop key = "add *"> propagation_required </prop>
<Prop key = "Update *"> propagation_required </prop>
<Prop key = "Save *"> propagation_required </prop>
<Prop key = "find *"> propagation_required, readonly </prop>
</Props>
</Property>
</Bean>
The specific module can be configured in this simple way.
You only need to specify its parent (parent class.
The parent class usually sets abstract = "true", because Initialization is not required when the container is loaded, and Initialization is performed when its subclass is called when it is used.
<Bean id = "fundservicedaoproxy" parent = "transactionbase">
<Property name = "target">
<Ref bean = "fundservice"/>
</Property>
</Bean>
If multiple modules such as fundservice are configured in this way, you can have less repeated code.
Method 3: Configure declarative transactions as follows. Mainly uses beannameautoproxycreator to automatically create a transaction proxy
<Bean id = "transactioninterceptor"
Class = "org. springframework. transaction. Interceptor. transactioninterceptor">
<Property name = "transactionmanager">
<Ref bean = "transactionmanager"/>
</Property>
<! -- Configure transaction properties -->
<Property name = "transactionattributes">
<Props>
<Prop key = "delete *"> propagation_required </prop>
<Prop key = "add *"> propagation_required </prop>
<Prop key = "Update *"> propagation_required </prop>
<Prop key = "Save *"> propagation_required </prop>
<Prop key = "find *"> propagation_required, readonly </prop>
</Props>
</Property>
</Bean>
<Bean
Class = "org. springframework. AOP. Framework. autoproxy. beannameautoproxycreator">
<Property name = "beannames">
<List>
<Value> fundservice </value>
</List>
</Property>
<Property name = "interceptornames">
<List>
<Value> transactioninterceptor </value>
</List>
</Property>
</Bean>
This method mainly utilizes the interceptor principle.
The first three methods must specify the specific module bean.
If there are too many modules, for example, a large website usually has dozens of modules, we have to consider the fourth configuration method. The transaction proxy is automatically created.
Method 4: Configure declarative transactions as follows.
<Bean id = "transactioninterceptor"
Class = "org. springframework. transaction. Interceptor. transactioninterceptor">
<Property name = "transactionmanager">
<Ref bean = "transactionmanager"/>
</Property>
</Bean>
<! -- Automatic proxy -->
<Bean id = "autoproxy"
Class = "org. springframework. AOP. Framework. autoproxy. beannameautoproxycreator">
<! -- It can be a service or Dao layer (preferably for the service layer * Service) -->
<Property name = "beannames">
<List>
<Value> * service </value>
</List>
</Property>
<Property name = "interceptornames">
<List>
<Value> transactioninterceptor </value>
</List>
</Property>
</Bean>
Automatic proxy is also used in combination with regular expressions and advice.
<Bean id = "transactioninterceptor"
Class = "org. springframework. transaction. Interceptor. transactioninterceptor">
<Property name = "transactionmanager">
<Ref bean = "transactionmanager"/>
</Property>
</Bean>
<Bean id = "autoproxycreator"
Class = "org. springframework. AOP. Framework. autoproxy. defaultadvisorautoproxycreator">
</Bean>
<Bean id = "regexpmethodpointcutadvisor"
Class = "org. springframework. AOP. Support. regexpmethodpointcutadvisor">
<Property name = "advice">
<Ref bean = "transactioninterceptor"/>
</Property>
<Property name = "pattern">
<Value>. * </value>
</Property>
</Bean>
This method can intercept and process transactions for specific modules.
In your actual project, you can choose different methods based on your situation.