Use aop to configure transactions

Source: Internet
Author: User

[Html] <? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns =" http://www.springframework.org/schema/beans "Xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance "Xmlns: aop =" http://www.springframework.org/schema/aop "Xmlns: tx =" http://www.springframework.org/schema/tx "Xsi: schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans Spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop Spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx /Spring-tx-2.0.xsd "> <bean id =" phrDS "class =" org. springframework. jdbc. datasource. driverManagerDataSource "> <property name =" driverClassName "value =" org. logicalcobwebs. proxool. proxoolDriver "/> <property name =" url "value =" proxool. phrConn "/> </bean> <bean id =" cdaDS "class =" org. springframework. jdbc. datasource. driverManagerDataSource "> <property name =" driverClassName "value =" org. logicalcobwebs. proxool. ProxoolDriver "/> <property name =" url "value =" proxool. cdaConn "/> </bean> <! -- CDA <bean id = "cdaDS" class = "org. springframework. jdbc. datasource. driverManagerDataSource "> <property name =" driverClassName "> <value> oracle. jdbc. driver. oracleDriver </value> </property> <property name = "url"> <value >$ {dbconn. cda. url} </value> </property> <property name = "username"> <value >$ {dbconn. cda. userName} </value> </property> <property name = "password"> <value >$ {dbconn. cda. password} </value> </prop Erty> </bean> --> <! -- Document database data source configuration-Oracle Data source configuration --> <bean id = "docstoreDS" class = "org. springframework. jdbc. datasource. driverManagerDataSource "> <property name =" driverClassName "> <value> oracle. jdbc. driver. oracleDriver </value> </property> <property name = "url"> <value >$ {dbconn.doc store. url} </value> </property> <property name = "username"> <value >$ {dbconn.doc store. userName} </value> </property> <property name = "password"> <value> Using mongodbconn.doc store. password} </value> </property> </bean> <bean id = "phrSessionFactory" class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean "> <property name =" dataSource "ref =" phrDS "/> <property name =" mappingDirectoryLocations "> <list> <value >$ {dbconn.doc store. mappingDirectory} </value> </list> </property> <property name = "hibernateProperties"> <props> <prop key = "hibernate. dialect ">$ {dbc Onn.doc store. dialect} </prop> <prop key = "hibernate. show_ SQL "> mongomongodbconn.doc store. showsql} </prop> <prop key = "hibernate. format_ SQL "> true </prop> <prop key =" hibernate. cache. use_second_level_cache "> false </prop> <prop key =" hibernate. cache. provider_class "> org. hibernate. cache. ehCacheProvider </prop> <prop key = "hibernate. cache. use_query_cache "> false </prop> <! -- <Prop key = "current_session_context_class"> jta </prop> <prop key = "hibernate. transaction. factory_class "> org. hibernate. transaction. JTATransactionFactory </prop> for jta compatilibility (jta) org. hibernate. context. JTASessionContext --> <prop key = "hibernate. cglib. use_reflection_optimizer "> true </prop> </props> </property> </bean> <bean id =" cdaSessionFactory "class =" org. springframework. orm. hibernate3. LocalSessionFactoryBean "> <property name =" dataSource "ref =" cdaDS "/> <property name =" mappingDirectoryLocations "> <list> <value >$ {dbconn.doc store. mappingDirectory} </value> </list> </property> <property name = "hibernateProperties"> <props> <prop key = "hibernate. dialect "> mongodbconn.doc store. dialect} </prop> <prop key = "hibernate. show_ SQL "> mongomongodbconn.doc store. showsql} </prop> <prop key = "hibernate. format_sq L "> true </prop> <prop key =" hibernate. cache. use_second_level_cache "> false </prop> <prop key =" hibernate. cache. provider_class "> org. hibernate. cache. ehCacheProvider </prop> <prop key = "hibernate. cache. use_query_cache "> false </prop> <! -- For jta compatilibility (jta) org. hibernate. context. JTASessionContext <prop key = "current_session_context_class"> jta </prop> <prop key = "hibernate. transaction. factory_class "> org. hibernate. transaction. JTATransactionFactory </prop> --> </props> </property> </bean> <! -- Template configuration --> <bean id = "phrJDBCTemplate" class = "org. springframework. jdbc. core. jdbcTemplate "> <property name =" dataSource "ref =" phrDS "/> </bean> <! -- HibernateTemplete configuration --> <bean id = "phrHibernateTemplete" class = "org. springframework. orm. hibernate3.HibernateTemplate "> <property name =" sessionFactory "ref =" phrSessionFactory "/> </bean> <! -- HibernateDAO configuration --> <bean id = "phrHibernateDao" class = "com. xbzc. phr. dao. hibernateSuperDAOImpl "> <property name =" sessionFactory "ref =" phrSessionFactory "/> </bean> <! -- HibernateDAO configuration-view and use CDA --> <bean id = "cdaHibernateDao" class = "com. xbzc. phr. dao. cdaHibernateSuperDAOImpl "> <property name =" sessionFactory "ref =" cdaSessionFactory "/> </bean> <span style =" color: # ff0000; "> <bean id =" springTransactionManager "class =" org. springframework. orm. hibernate3.HibernateTransactionManager "> <property name =" sessionFactory "ref =" phrSessionFactory "/> </bean> <! -- Notification configuration --> <tx: advice id = "txAdvice" transaction-manager = "springTransactionManager"> <tx: attributes> <tx: method name = "save *" propagation = "REQUIRED" rollback-for = "DaoException"/> </tx: attributes> </tx: advice> <! -- Transaction aspect configuration, configure the classes involved in the transaction --> <aop: config> <aop: pointcut id = "point" expression = "execution (* com. xbzc. phr. service. um. userManageSrv. *(..)) "/> <! -- Specify classes and methods for using jta transactions --> <aop: advisor advice-ref = "txAdvice" pointcut-ref = "point"/> </aop: config> </span> </beans> above is my configuration file, mainly red code. rollback-for = "DaoException" is very important, here, the exception I specified for rollback is my custom exception DaoException. Therefore, if an uncaptured exception is thrown in the program, the transaction will be rolled back. See the following code: [java] public RPCMessage saveUser (TUmUserDTO entity) <span style = "color: # ff0000;"> throws DaoException </span> {log.info ("service layer operation: add a practitioner saveUser (TUmUserDTO ["+ entity +"]) "); Try {if (entity = null) {throw new ServiceException (ServiceException. ERRORCODE_PARAM_NULL);} If rollback-for = "ServiceException" is caught in the program, the transaction will not be rolled back because the exception is swallowed up and the transaction has been committed, see the following code: [java] log. debug ("service layer operation: practitioner role edited"); return this. getRPCMessage ();} catch (<span style = "color: # ff0000;"> ServiceException </span> e) {return this. getRPCMessage (e);} If rollback-for = "Exception", it belongs to the Exception and Exception packets. All exceptions are rolled back by transactions. Here, because DaoException and ServiceException are inherited from Exception, they are also rolled back. Expression = "execution (* com. xbzc. phr. service. um. userManageSrv. *(..)) "/> is used to determine which classes of methods are used when a transaction is rolled back. If rollback-for = "" is not configured, only operations that are caused by exceptions that are not captured are rolled back by default.

Related Article

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.