使用TransactionTemplate
2008年04月24日 星期四 14:30
----------------------------------------------------------------------------------------------------------
Spring编程式事务管理
直接采用JDBC操作数据库
使用TransactionTemplate,它封裝了事務流程,透過callback來完成事務,例如:
-----------------------------------------------------------------------------------------------------------
drop TABLE sampledb.orders;
CREATE TABLE sampledb.orders(
`ID` BIGINT NOT NULL,
`ORDER_NUMBER` VARCHAR(15),
`CUSTOMER_ID` BIGINT,
PRIMARY KEY(`ID`)
)
ENGINE = InnoDB;
drop TABLE `sampledb`.`customers`;
CREATE TABLE `sampledb`.`customers` (
`ID` BIGINT NOT NULL,
`NAME` VARCHAR(45),
PRIMARY KEY(`ID`)
)
ENGINE = InnoDB;
alter table sampledb.orders
add constraint constraint_constraint_fk
foreign key (CUSTOMER_ID)
references sampledb.customers(ID);
-----------------------------------------------------------------------------------------------------------
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-2.0.xsd">
<!-- 定义DataSource -->
<bean
>
<property value="com.mysql.jdbc.Driver" />
<property
value="jdbc:mysql://localhost:3306/SAMPLEDB" />
<property value="root" />
<property value="" />
</bean>
<bean
>
<property ref="dataSource" />
</bean>
<bean >
<property >
<ref bean="dataSource" />
</property>
<property >
<ref bean="transactionManager" />
</property>
</bean>
</beans>
-----------------------------------------------------------------------------------------------------------
UserDAO.java
-----------------------------------------------------------------------------------------------------------
package db;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
public class UserDAO {
private DataSource dataSource;
private PlatformTransactionManager transactionManager;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void setTransactionManager(
PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public void insertUser(final Customers c) {
TransactionTemplate transactionTemplate = new TransactionTemplate(
transactionManager);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(TransactionStatus status) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "insert into customers(id, name) values(";
sql += c.getId();
sql += ",'" + c.getName() + "'";
sql += ")";
System.out.println(sql);
jdbcTemplate.update(sql);
}
});
}
public void queryAllBooks() {
List<Customers> customers = new JdbcTemplate(dataSource).query(
"select * from customers", new CustomerRowMapper());
System.err.println("-- All Customers ---------------------------");
for (Customers customer : customers) {
System.err.println(" " + customer.getName() + ", by "
+ customer.getName());
}
System.err.println("----------------------------------------");
}
}
class CustomerRowMapper implements RowMapper {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Customers book = new Customers();
book.setId(Long.parseLong(rs.getString("id")));
book.setName(rs.getString("name"));
return book;
}
}
-----------------------------------------------------------------------------------------------------------
Demo.java
-----------------------------------------------------------------------------------------------------------
package db;
import javax.sql.DataSource;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Demo {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(
"applicationContext.xml"));
DataSource dataSource = (DataSource) factory.getBean("dataSource");
UserDAO dao = (UserDAO) factory.getBean("userDAO");
Customers c = new Customers();
c.setId(5l);
c.setName("xiong");
dao.insertUser(c);
dao.queryAllBooks();
System.out.println("END");
}
}
-----------------------------------------------------------------------------------------------------------
Spring编程式事务管理
直接采用JDBC操作数据库
使用TransactionTemplate,它封裝了事務流程,透過callback來完成事務,例如:
-----------------------------------------------------------------------------------------------------------
drop TABLE sampledb.orders;
CREATE TABLE sampledb.orders(
`ID` BIGINT NOT NULL,
`ORDER_NUMBER` VARCHAR(15),
`CUSTOMER_ID` BIGINT,
PRIMARY KEY(`ID`)
)
ENGINE = InnoDB;
drop TABLE `sampledb`.`customers`;
CREATE TABLE `sampledb`.`customers` (
`ID` BIGINT NOT NULL,
`NAME` VARCHAR(45),
PRIMARY KEY(`ID`)
)
ENGINE = InnoDB;
alter table sampledb.orders
add constraint constraint_constraint_fk
foreign key (CUSTOMER_ID)
references sampledb.customers(ID);
-----------------------------------------------------------------------------------------------------------
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-2.0.xsd">
<!-- 定义DataSource -->
<bean
>
<property value="com.mysql.jdbc.Driver" />
<property
value="jdbc:mysql://localhost:3306/SAMPLEDB" />
<property value="root" />
<property value="" />
</bean>
<bean
>
<property ref="dataSource" />
</bean>
<bean >
<property >
<ref bean="dataSource" />
</property>
<property >
<ref bean="transactionManager" />
</property>
</bean>
</beans>
-----------------------------------------------------------------------------------------------------------
UserDAO.java
-----------------------------------------------------------------------------------------------------------
package db;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
public class UserDAO {
private DataSource dataSource;
private PlatformTransactionManager transactionManager;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void setTransactionManager(
PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public void insertUser(final Customers c) {
TransactionTemplate transactionTemplate = new TransactionTemplate(
transactionManager);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(TransactionStatus status) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "insert into customers(id, name) values(";
sql += c.getId();
sql += ",'" + c.getName() + "'";
sql += ")";
System.out.println(sql);
jdbcTemplate.update(sql);
}
});
}
public void queryAllBooks() {
List<Customers> customers = new JdbcTemplate(dataSource).query(
"select * from customers", new CustomerRowMapper());
System.err.println("-- All Customers ---------------------------");
for (Customers customer : customers) {
System.err.println(" " + customer.getName() + ", by "
+ customer.getName());
}
System.err.println("----------------------------------------");
}
}
class CustomerRowMapper implements RowMapper {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Customers book = new Customers();
book.setId(Long.parseLong(rs.getString("id")));
book.setName(rs.getString("name"));
return book;
}
}
-----------------------------------------------------------------------------------------------------------
Demo.java
-----------------------------------------------------------------------------------------------------------
package db;
import javax.sql.DataSource;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Demo {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(
"applicationContext.xml"));
DataSource dataSource = (DataSource) factory.getBean("dataSource");
UserDAO dao = (UserDAO) factory.getBean("userDAO");
Customers c = new Customers();
c.setId(5l);
c.setName("xiong");
dao.insertUser(c);
dao.queryAllBooks();
System.out.println("END");
}
}
-----------------------------------------------------------------------------------------------------------
本篇日志被作者设置为禁止发表新评论
©2008 Baidu
引文来源 使用TransactionTemplate_熊熊之家