2014년 1월 31일 금요일

[스프링3트랜잭션강좌/예제]Spring Framework3.X TransactionManager이용한프로그래밍적트랜잭션예제,spring framework3.X transaction, 스프링교육강좌,스프링프레임워크강좌교육

[스프링3트랜잭션강좌/예제]Spring Framework3.X TransactionManager이용한프로그래밍적트랜잭션예제,spring framework3.X transaction, 스프링교육강좌,스프링프레임워크강좌교육

import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
public class TrTest1 {
DataSource dataSource;
PlatformTransactionManager transactionManager;
public static void main(String[] args) {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
ds.setUrl("jdbc:oracle:thin:@192.168.0.7:1521:onj");
ds.setUsername("scott");
ds.setPassword("tiger");

DataSourceTransactionManager tm = new DataSourceTransactionManager();
tm.setDataSource(ds);
TrTest1 update = new TrTest1();
update.setDataSource(ds);
update.setTransactionManager(tm);
// execute 
update.doUpdate();
System.out.println("<<<<<<<<<<<<< TR OK >>>>>>>>>>>>");
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void setTransactionManager(
PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public void doUpdate() {
DefaultTransactionDefinition td = new DefaultTransactionDefinition(
TransactionDefinition.PROPAGATION_REQUIRED);
td.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
td.setTimeout(10);
TransactionStatus status = transactionManager.getTransaction(td);
try {
updateSal(3000);
} catch (DataAccessException e) {
transactionManager.rollback(status);
throw e;
}
transactionManager.commit(status);
}
private void updateSal(int newSal) throws DataAccessException {
String strQuery = "update emp set sal = " + newSal + " where ename = 'SMITH'";
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update(strQuery);
}
}

댓글 없음:

댓글 쓰기