spring中JDBC编程式事务
来源:百度文库 编辑:神马文学网 时间:2024/06/30 19:34:23
在spring采用纯JDBC进行数据库操作:applicationContext.xmlview plaincopy to clipboardprint?
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-2.0.xsd">
org.gjt.mm.mysql.Driver
jdbc:mysql://localhost:3306/test
root
root
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-2.0.xsd">
org.gjt.mm.mysql.Driver
jdbc:mysql://localhost:3306/test
root
root
测试代码:view plaincopy to clipboardprint?
package com.spring.jdbc;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
/**
* JDBC编程式事务控制
* @author nk
*
*/
public class JdbcTemplateTest {
public static void main(String[] args) {
Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
final JdbcTemplate jdbcTemplate = (JdbcTemplate)factory.getBean("jdbcTemplate");
//不会被回滚
jdbcTemplate.execute("insert into test(name,age) values('Tom2',20)");
TransactionTemplate transactionTemplate = (TransactionTemplate)factory.getBean("transactionTemplate");
//回调方法中的jdbc操作,如果未发生异常则会自动提交,发生异常则会回滚
transactionTemplate.execute(new TransactionCallbackWithoutResult(){
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
jdbcTemplate.execute("insert into test(name,age) values('Tom',20)");
jdbcTemplate.execute("insert into test(name,ages) values('Tom',20)");
} catch (Exception e) {
status.setRollbackOnly(); //回滚
System.out.println("回滚事务");
}
}
});
}
} 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yeson6/archive/2009/12/07/4954589.aspx
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-2.0.xsd">
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-2.0.xsd">
测试代码:view plaincopy to clipboardprint?
package com.spring.jdbc;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
/**
* JDBC编程式事务控制
* @author nk
*
*/
public class JdbcTemplateTest {
public static void main(String[] args) {
Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
final JdbcTemplate jdbcTemplate = (JdbcTemplate)factory.getBean("jdbcTemplate");
//不会被回滚
jdbcTemplate.execute("insert into test(name,age) values('Tom2',20)");
TransactionTemplate transactionTemplate = (TransactionTemplate)factory.getBean("transactionTemplate");
//回调方法中的jdbc操作,如果未发生异常则会自动提交,发生异常则会回滚
transactionTemplate.execute(new TransactionCallbackWithoutResult(){
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
jdbcTemplate.execute("insert into test(name,age) values('Tom',20)");
jdbcTemplate.execute("insert into test(name,ages) values('Tom',20)");
} catch (Exception e) {
status.setRollbackOnly(); //回滚
System.out.println("回滚事务");
}
}
});
}
} 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yeson6/archive/2009/12/07/4954589.aspx
spring中JDBC编程式事务
在Spring中实现事务挂起
在Spring中集成Hibernate事务
在Spring中配置Hibernate事务
在Spring中实现事务挂起
在Spring中配置Hibernate事务
全面分析 Spring 的编程式事务管理 及声明式事务管理
解惑 spring 嵌套事务
spring声明式事务
Spring 事务配置
Spring源代码分析(三):Spring JDBC
Spring源代码分析(三):Spring JDBC
spring 声明试事务代理
使用JDBC进行数据访问【spring framwork】
JTA初级研究之JTA和JDBC事务
spring事务配置的奇技淫巧[赞]
spring下hibernate事务配置范例
Spring下Hibernate事务配置范例
如何使用spring声明式事务
解惑 spring 嵌套事务 转载自JavaEye Java版 王政
Spring的四种声明式事务的配置-Hibernate事务
Spring的四种声明式事务的配置-Hibernate事务
源代码解读Spring只读事务与读写事务的性能的差别
加快JDBC设计中JSP访问数据库