Use spring's transaction template to manually roll back a transaction

Source: Internet
Author: User


In the spring configuration file, configure the bean corresponding to the transaction template class

	<!--manual Transaction template--
	<bean id= "transactiontemplate" class= " Org.springframework.transaction.support.TransactionTemplate ">
		<property name=" TransactionManager "ref= "Txmgr"/>
	</bean>
Using the transaction template in service, manually rollback the current transaction where a transaction rollback is required, as shown in the following code:
Message msg = Transactiontemplate.execute (new transactioncallback<message> () {
		@Override
		public Message dointransaction (transactionstatus status) {
			message returnmsg = new Message ();
			try {
				returnmsg = debitinfoservice.domoddebitinfowhenprocessfinish (requestmap);
				if (!returnmsg.issuccess ()) {
					status.setrollbackonly ();
				}
			} catch (Exception e) {
				Returnmsg.setsuccess (false);
				Returnmsg.setmsg (Exceptionutils.getmessage (e));
				Returnmsg.setvalue (Exceptionutils.getstacktrace (e));
				Status.setrollbackonly ();
			}
			return returnmsg;
		}
	});

If processing errors, log if
(!msg.issuccess ()) {
	loggerutil.error ("Debit Return", Msg.getmsg (), Msg.getvalue ());
}

if (msg.issuccess ()) {
	resultmap.put ("status", "1");
} else{
	resultmap.put ("status", "0");
}


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.