Transaction: Transaction strengthen AccountService achieved through dynamic transfer agent interface
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置代理的service --> <bean id="proxyAccountService" factory-bean="beanFactory" factory-method="getAccountService"></bean> <!- the bean<->Configuration BeanFactory id="beanFactory" class="com.hdh.factory.BeanFactory"> <!-- 注入service --> <property name="as" ref="accountService"></property> <!-- 注入事务管理器 --> <property name="tm" ref="txManager"></property> </bean> <!--配置service --> <bean id="accountService" class="com.hdh.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> </bean> <!--配置dao --> <bean id="accountDao" class="com.hdh.dao.impl.AccountDaoImpl"> <!-- 注入QueryRunner --> <property name="runner" ref="runner"></property> <!-- 注入ConnectionUtils --> <property name="connectionUtils" ref="connectionUtils"></property> </bean> <!--配置QueryRunner --> <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"></bean> <!-- 配置Connection的工具类 ConnectionUtils --> <bean id="connectionUtils" class="com.hdh.utils.ConnectionUtils"> <!-- 注入数据源 --> <property name="dataSource" ref="dataSource"></property> </the bean > <-! configuration data Source -> < the bean ID = "the dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" > <-! connection information database essential -> < Property name = "driverClass" value = "com.mysql.jdbc.Driver" > </ Property > < Property name = "the jdbcUrl" value = "JDBC: MySQL: // localhost: 3306 / Spring" > </ Property > < Property name = "user" value="root"></property> <property name="password" value="12345"></property> </bean> <!-- 配置事务管理器--> <bean id="txManager" class="com.hdh.utils.TransactionManager"> <!-- 注入ConnectionUtils --> <property name="connectionUtils" ref="connectionUtils"></property> </bean> </beans>
2.ConnectionUtils.java // get a connection on the current thread
/ ** * connecting tools, which is used to obtain a connection from the data source, and the binding threads and to achieve * / public class ConnectionUtils { Private the ThreadLocal <Connection> TL = new new the ThreadLocal <Connection> (); Private the DataSource the dataSource; public void the setDataSource (the DataSource the dataSource) { the this .DataSource = ; the dataSource } / ** * Get the current thread connection * @return * / public connection getThreadConnection () { the try { // Get 1. start with the ThreadLocal connection conn =tl.get (); // 2. determining whether the current thread connection IF (Conn == null ) { // 3. obtain a connection from the data source and stored in a ThreadLocal Conn = dataSource.getConnection (); tl.set (Conn); } // 4. return connection on the current thread return Conn; } the catch (Exception E) { the throw new new a RuntimeException (E); } } / ** * connecting thread and unbundling * / public void removeConnection () { tl.remove (); } }
3.TranscationManager.java
/ ** * transaction management and related tools, which contains, open transaction, committing the transaction, and transaction rollback releasable connection * / public class the TransactionManager { Private ConnectionUtils connectionUtils; public void setConnectionUtils (ConnectionUtils connectionUtils) { the this .connectionUtils = connectionUtils; } / ** * open transaction * / public void beginTransaction () { the try { connectionUtils.getThreadConnection () the setAutoCommit (. to false ); } the catch (Exception E) { e.printStackTrace (); } } / ** Submit transaction * * / public void the commit () { the try { connectionUtils.getThreadConnection () the commit ();. } The catch (Exception E) { e.printStackTrace (); } } / ** * rollback transaction * / public void ROLLBACK () { the try { connectionUtils.getThreadConnection () ROLLBACK ();. } the catch (Exception E) { e.printStackTrace (); } } / ** * releasable connection * / public void release(){ try { connectionUtils.getThreadConnection().close();//还回连接池中 connectionUtils.removeConnection(); }catch (Exception e){ e.printStackTrace(); } } }
4. strengthening the dynamic proxy interface AccountService
/** * 用于创建Service的代理对象的工厂 */ public class BeanFactory { private AccountService as; private TransactionManager tm; public void setAs(AccountService as) { this.as = as; } public void setTm(TransactionManager tm) { this.tm = tm; } public AccountService getAccountService() { return (AccountService) Proxy.newProxyInstance(as.getClass().getClassLoader(), as.getClass().getInterfaces(), new InvocationHandler() { publicInvoke Object (Object Proxy, Method, Method, Object [] args) throws the Throwable { Object Invoke = null ; the try { // Open things tm.beginTransaction (); Invoke = Method.invoke (AS, args); // transaction commits tm .commit (); } the catch (Exception E) { // things rollback tm.rollback (); }the finally { // release the connection tm.release (); } return Invoke; } }); } }