spring中JDBC编程式事务
来源:百度文库 编辑:神马文学网 时间:2024/10/01 08:44:57
在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