小明博客 > 技术文案 >
Spring 编程事物管理
文章来源:忆安 时间:2025-03-28
除Spring的DIST停的包中,参加:
commons-pool.jarcommons-dbcp.jarmysql-connector-java-5.1.5-bin.jar那里应用的是mysql数据库,正在test库内乱创立表:
DROPTABLEIFEXISTS`user`;CREATETABLE`user`(`id`int(11)NOTNULLAUTO_INCREMENT,`age`int(11)DEFAULTNULL,`name`varchar(255)DEFAULTNULL,PRIMARYKEY(`id`))ENGINE=InnoDBDEFAULTCHARSET=utf8;编程式什物绝对解释式实物有些烦琐,然则照样有其独到的长处。编程式的实物办理能够分明的操纵工作的鸿沟,自止操纵什物最先、撤消、超时、停止等,自在操纵实物的颗粒度。滥用Spring MVC 初学示例http://www.javacui.com/Framework/224.html的代码。那里曲交正在Action层曲交干代码示例,并应用讲明停止属性注进:
起首编写applicationContext.xml,设置数据库毗连属性:
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><!--数据源--><beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"><propertyname="driverClassName"value="com.mysql.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"/><propertyname="username"value="root"/><propertyname="password"value="root"/></bean></beans>Spring供应二种实行体例,应用PlatformTransactionManager或者TransactionTemplate。以停示例应用PlatformTransactionManager的完毕类DataSourceTransactionManager去告终。
packagetest;importjava.util.Date;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.sql.DataSource;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.jdbc.core.JdbcTemplate;importorg.springframework.jdbc.datasource.DataSourceTransactionManager;importorg.springframework.transaction.TransactionStatus;importorg.springframework.transaction.support.DefaultTransactionDefinition;importorg.springframework.web.bind.annotation.RequestMapping;//http://localhost:8080/spring/hello.do?user=java@org.springframework.stereotype.ControllerpublicclassHelloController{privateDataSourceTransactionManagertransactionManager;privateDefaultTransactionDefinitiondef;privateJdbcTemplatejdbcTemplate;@SuppressWarnings("unused")//应用注脚注进属性@AutowiredprivatevoidsetDataSource(DataSourcedataSource){jdbcTemplate=newJdbcTemplate(dataSource);transactionManager=newDataSourceTransactionManager(dataSource);//什物界说def=newDefaultTransactionDefinition();//实物传达特征def.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED);//def.setReadOnly(true);//指定后会干少少劣化操纵,然则必需配搭传达特色,比方:PROPAGATION_REQUIRED,PROPAGATION_REQUIRES_NEW,PROPAGATION_NESTED//def.setTimeout(1000);//公道的超通常间,有帮于体系越发无效率}@SuppressWarnings("deprecation")@RequestMapping("/hello.do")publicStringhello(HttpServletRequestrequest,HttpServletResponseresponse){request.setAttribute("user",request.getParameter("user")+"-->"+newDate().toLocaleString());TransactionStatusstatus=transactionManager.getTransaction(def);try{jdbcTemplate.update("updateusersetage=age+1;");//产生同常jdbcTemplate.update("updateusersetage='test';");transactionManager.commit(status);}catch(Exceptione){transactionManager.rollback(status);}return"hello";}}也能够应用TransactionTemplate去实行,它须要1个TransactionManager真例,代码以下:
packagetest;importjava.util.Date;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.sql.DataSource;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.jdbc.core.JdbcTemplate;importorg.springframework.jdbc.datasource.DataSourceTransactionManager;importorg.springframework.transaction.TransactionStatus;importorg.springframework.transaction.support.DefaultTransactionDefinition;importorg.springframework.transaction.support.TransactionCallback;importorg.springframework.transaction.support.TransactionTemplate;importorg.springframework.web.bind.annotation.RequestMapping;@org.springframework.stereotype.ControllerpublicclassHelloController{privateDataSourceTransactionManagertransactionManager;privateDefaultTransactionDefinitiondef;privateJdbcTemplatejdbcTemplate;@SuppressWarnings("unused")@AutowiredprivatevoidsetDataSource(DataSourcedataSource){jdbcTemplate=newJdbcTemplate(dataSource);transactionManager=newDataSourceTransactionManager(dataSource);def=newDefaultTransactionDefinition();def.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED);}@SuppressWarnings({"deprecation","unchecked"})@RequestMapping("/hello.do")publicStringhello(HttpServletRequestrequest,HttpServletResponseresponse){request.setAttribute("user",request.getParameter("user")+"-->"+newDate().toLocaleString());TransactionTemplatetransactionTemplate=newTransactionTemplate(transactionManager);Objectobj=null;try{//没有须要前往值应用TransactionCallbackWithoutResultbackobj=transactionTemplate.execute(newTransactionCallback(){publicObjectdoInTransaction(TransactionStatusarg0){jdbcTemplate.update("updateusersetage=age+1;");//爆发同常jdbcTemplate.update("updateusersetage='test';");return1;}});}catch(Exceptione){e.printStackTrace();}System.out.println(obj);return"hello";}}注重,没有要再doInTransaction内乱干同常逮捉,不然没法操纵实物。
推举您浏览更多相关于“ springmvc实物撤消applicationContext ”的作品