Configure transaction processing when spring integrates hibernate

Source: Internet
Author: User

1. first. configure a context-Param node in XML to allow the project to have multiple spring configuration files. If there are many projects, the project may be divided into many modules, each module uses an applicationcontext-level name-Module name. XML, such as applicationContext-service-som.xml, applicationContext-action-som.xml, etc.

 

<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>/WEB-INF/applicationcontext *. xml </param-value>
</Context-param>

 

2. In the configuration file of the hibernate data source (named applicationContext-resource.xml here)

 

<! -- Some model configurations -->

<Bean id = "sessionfactory"

Class = "org. springframework. Orm. hibernate3.localsessionfactorybean">
<Property name = "datasource" ref = "performancedemo"/>
<Property name = "mappingresources">

<Property name = "mappingresources">
<List>
<Value> COM/sshdemo/ERP/SOM/model/somxxxxx. HBM. xml </value>
<Value> COM/sshdemo/ERP/IVC/model/ivcxxxxx. HBM. xml </value>
<Value> COM/sshdemo/ERP/IVC/model/ivcxxxxxx. HBM. xml </value>

</List>
</Property>
<Property name = "hibernateproperties">
<Props>
<Prop key = "hibernate. dialect"> org. hibernate. dialect. oracle9dialect </prop>
<Prop key = "hibernate. show_ SQL"> false </prop>
<Prop key = "hibernate. Connection. release_mode"> auto </prop>
<Prop key = "hibernate. transaction. auto_close_session"> true </prop>
<Prop key = "show_ SQL"> true </prop>
</Props>
</Property>

</Bean>

<! -- Transaction Manager for a single hibernate sessionfactory (alternative to JTA) -->
<Bean id = "transactionmanager" class = "org. springframework. Orm. hibernate3.hibernatetransactionmanager">
<Property name = "sessionfactory" ref = "sessionfactory"/>
</Bean>

<! -- Doing standard crud configuration hibernatetemplate. base classes of all DaO layer classes -->
<Bean id = "basedaohibernate" class = "com. synnex. ERP. Core. Dao. basedaohibernate">
<Property name = "sessionfactory" ref = "sessionfactory"/>
</Bean>

<! -- Configure jdbctemplate -->

<Bean id = "jdbcwrapper" class = "org. springframework. JDBC. Core. jdbctemplate">
<Property name = "datasource" ref = "performancedemo"/>
</Bean>

<! -- Configure the base class of the transaction processing class at the service layer -->

<Bean id = "basetxservice" class = "org. springframework. transaction. Interceptor. transactionproxyfactorybean" abstract = "true">
<Property name = "transactionmanager" ref = "transactionmanager"/>
<Property name = "transactionattributes">
<Props>
<Prop key = "find *"> propagation_required, readonly </prop>
<Prop key = "insert *"> propagation_required </prop>
<Prop key = "Update *"> propagation_required </prop>
<Prop key = "Remove *"> propagation_required </prop>
</Props>
</Property>
</Bean>

 

 

3. After the data source is configured, you can now configure the service layer class that requires transaction protection. Assume that the interface class somxxxxservice of the class, and configure the service layer to follow the interface name-implementation class method. Configure a ** TX class for this class (in fact, it is still the class itself, but it is an instance for Real Transaction Processing). The Dao configuration is omitted here.

 

<Bean name = "somofdeservice"
Class = "com. synnex. ERP. CG. Som. Service. impl. somxxxxserviceimpl">
<Property name = "kimxxxxdao" ref = "kimxxxxdao"/>

<Property name = "somxxxxservicetx" ref = "somxxxxservicetx"/>
</Bean>

<! -- The basetxservice here is the transaction base class configured by the applicationContext-resource.xml above -->

<Bean id = "somxxxxservicetx" parent = "basetxservice"
Singleton = "false">
<Property name = "target">
<Bean
Class = "com. synnex. ERP. CG. Som. Service. impl. somxxxxserviceimpl">
<Property name = "kimxxxxdao" ref = "kimxxxxdao"/>
</Bean>
</Property>
<Property name = "transactionattributes">
<Props>

<! -- The key here is the method name in servicetx that requires transaction protection -->
<Prop key = "dobatchinserttx"> propagation_required </prop>
<Prop key = "dodeletetx"> propagation_required </prop>
<Prop key = "doupdatetx"> propagation_required </prop>
</Props>
</Property>
</Bean>

 

4. After the configuration is complete, the somxxxxservicetx. dobatchinserttx Runtime is protected by transactions.

 

Note: For Lightweight J2EE development, Hibernate + spring is generally used. Transaction protection is basically compared to the work done. Here is a summary.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.