1 package com.freemindcafe.jdbc.sample4;
3 import java.io.IOException;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import java.util.ArrayList;
9 import junit.framework.Assert;
11 import org.
apache.commons.dbcp.BasicDataSource;
12 import org.junit.Test;
13 import org.springframework.dao.DataAccessException;
14 import org.springframework.jdbc.core.JdbcTemplate;
15 import org.springframework.jdbc.core.ResultSetExtractor;
16 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
17 import org.springframework.transaction.TransactionStatus;
18 import org.springframework.transaction.support.TransactionCallback;
19 import org.springframework.transaction.support.TransactionTemplate;
24 public void test()
throws SQLException, IOException{
26 BasicDataSource dataSource=
new BasicDataSource();
27 dataSource.setUrl(
"jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=XE)))");
28 dataSource.setDriverClassName(
"oracle.jdbc.OracleDriver");
29 dataSource.setUsername(
"system");
30 dataSource.setPassword(
"neha1");
31 dataSource.setMaxActive(2);
32 dataSource.setMaxIdle(2);
33 dataSource.setMaxOpenPreparedStatements(2);
34 dataSource.setMaxWait(1000l);
35 dataSource.setPoolPreparedStatements(
true);
36 final JdbcTemplate jdbcTemplate =
new JdbcTemplate(dataSource);
37 final List<Integer> list=
new ArrayList<Integer>();
38 String query=
"Select 1 a from dual where 2="
39 +
"(select 2 from dual where 3="
40 +
"(select 3 from dual where 4="
41 +
"(select 4 from dual)))";
42 Boolean exception=
false;
46 jdbcTemplate.query(query,
new ResultSetExtractor<Object> (){
48 public Object extractData(ResultSet rs)
throws SQLException,
52 jdbcTemplate.query(
"select 2 from dual",
new ResultSetExtractor<Object> (){
54 public Object extractData(ResultSet arg0)
55 throws SQLException, DataAccessException {
57 jdbcTemplate.query(
"select 3 from dual",
new ResultSetExtractor<Object> (){
59 public Object extractData(ResultSet arg0)
60 throws SQLException, DataAccessException {
62 list.add(arg0.getInt(1));
67 return new Boolean(
true);
72 return new Boolean(
true);
81 Assert.assertTrue(exception);
84 TransactionTemplate transactionTemplate=
new TransactionTemplate();
85 DataSourceTransactionManager transactionManager =
new DataSourceTransactionManager(dataSource);
86 transactionTemplate.setTransactionManager(transactionManager );
88 transactionTemplate.execute(
new TransactionCallback<Object>() {
90 public Object doInTransaction(TransactionStatus arg0) {
91 return jdbcTemplate.query(
"select 1 from dual",
new ResultSetExtractor<Object> (){
92 public Object extractData(ResultSet rs)
throws SQLException,DataAccessException {
96 jdbcTemplate.query(
"select 2 from dual",
new ResultSetExtractor<Object> (){
98 public Object extractData(ResultSet arg0)
99 throws SQLException, DataAccessException {
101 jdbcTemplate.query(
"select 3 from dual",
new ResultSetExtractor<Object> (){
103 public Object extractData(ResultSet arg0)
104 throws SQLException, DataAccessException {
106 list.add(arg0.getInt(1));
111 return new Boolean(
true);
116 return new Boolean(
true);
125 Assert.assertEquals(list.size(), 1);
127 Assert.assertEquals((Integer)list.get(0),
new Integer(3));