2014년 5월 27일 화요일

[스프링3트랜잭션강좌/예제]TransactionManager이용한프로그래밍적트랜잭션예제,spring framework3.X transaction[자바개발자교육/자바교육/자바강좌/자바,Spring교육잘하는곳/자바,spring교육추천/자바실무교육/JAVA/JAVA교육/JAVA스프링학원/JAVA실무교육] 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); } } [출처] 오라클자바커뮤니티 - http://www.oraclejavacommunity.co.kr/bbs/board.php?bo_table=LecSpring&wr_id=373 오라클자바커뮤니티교육센터, 개발자전문교육, 개인80%환급 www.oraclejavacommunity.com 평일주간(9:30~18:20) 개강 (5/28)[교육전취업확정]Spring,MyBatis,XPlatform실무프로젝트과정 (5/30)[기업100%환급]자바기초에서 JDBC, Servlet/JSP까지 (5/30)[기업100%환급]SQL기초에서 Schema Object까지 (6/09)[기업100%환급]PL/SQL,ORACLE HINT,TUNING (6/09)[기업100%환급]안드로이드개발자과정 (6/09)[기업100%환급]Spring ,MyBatis,Hibernate실무과정 (6/16)[기업100%환급]C#4.0,WinForm,ADO.NET프로그래밍 평일야간(19:00~21:50) 개강 (5/28)Spring3.X, MyBatis, Hibernate실무과정 (5/28)SQL초보에서실전전문가까지 (5/29)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지 (6/03)안드로이드개발자과정 (6/03)웹퍼블리싱 마스터 (6/10)C#4.0, ADO.NET, Network 프로그래밍 (6/19)C#,ASP.NET마스터 주말(10:00~17:50) 개강 (5/31)Spring3.X, MyBatis, Hibernate실무과정 (5/31)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지 (5/31)SQL초보에서실전전문가까지 (5/31)C#,ASP.NET마스터 (5/31)실무예제로 배워보는 jQuery(개발자/디자이너를위한) (5/31)안드로이드개발자과정 주말저녁(18:30~22:20) 개강 (6/21)JAVA,Network&WEB&Framework (6/21)SQL기초에서실무까지

[스프링3트랜잭션강좌/예제]TransactionManager이용한프로그래밍적트랜잭션예제,spring framework3.X transaction[자바개발자교육/자바교육/자바강좌/자바,Spring교육잘하는곳/자바,spring교육추천/자바실무교육/JAVA/JAVA교육/JAVA스프링학원/JAVA실무교육]

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);
}
}

댓글 없음:

댓글 쓰기