1 import junit.framework.Test; 2 import junit.framework.TestSuite; 3 import junit.framework.TestCase; 4 5 import java.util.Properties ; 6 7 import org.enhydra.jdbc.pool.StandardXAPoolDataSource; 8 import org.enhydra.jdbc.standard.StandardXADataSource; 9 import org.objectweb.jotm.Jotm; 10 import org.objectweb.transaction.jta.TMService; 11 12 import javax.naming.InitialContext ; 13 import javax.naming.Context ; 14 import javax.transaction.UserTransaction ; 15 import java.util.Properties ; 16 import java.sql.ResultSet ; 17 import java.sql.PreparedStatement ; 18 import java.sql.Connection ; 19 import java.sql.Statement ; 20 21 public class XAPoolTestCase extends TestCase { 22 23 public String login = null; 24 public String password = null; 25 public String url = null; 26 public String driver = null; 27 public TMService jotm; 28 public String USER_TRANSACTION_JNDI_NAME = "UserTransaction"; 29 public UserTransaction utx = null; 30 public StandardXAPoolDataSource spds; 31 public StandardXADataSource xads; 32 public Connection conn; 33 public String SQL_REQUEST = "select id, foo from testdata where id=1"; 34 public String SQL_QUERY = "update testdata set foo = ? where id=1"; 35 public String datasourceClassName; 36 37 public XAPoolTestCase(String strTestName) { 38 super(strTestName); 39 } 41 42 public static Test suite() { 43 return new TestSuite(XAPoolTestSuite.class); 44 } 45 46 protected void setUp() throws Exception { 47 Properties prop = new Properties (); 50 try { 51 prop.load(ClassLoader.getSystemResourceAsStream("spy.properties")); 52 } catch (Exception e) { 53 System.err.println("problem to load properties."); 54 e.printStackTrace(); 55 throw e; 56 } 57 58 login = prop.getProperty("login"); 59 password = prop.getProperty("password"); 60 url = prop.getProperty("url"); 61 driver = prop.getProperty("driver"); 62 63 datasourceClassName = prop.getProperty("datasource-class", 66 StandardXADataSource.class.getName()); 67 68 try { 70 jotm = new Jotm(true, false); 72 } catch (Exception e) { 75 System.err.println("JOTM problem."); 76 e.printStackTrace(); 77 throw e; 78 } 79 80 81 try { 82 utx = jotm.getUserTransaction(); 85 } catch (Exception e) { 86 System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown"); 87 System.err.println("Exception message :" + e.getMessage()); 88 e.printStackTrace(); 89 throw e; 90 } 91 92 spds = new StandardXAPoolDataSource(2); 94 spds.setMaxSize(15); 95 spds.setMinSize(13); 96 spds.setUser(login); 97 spds.setPassword(password); 98 99 Class datasourceClass=Class.forName(datasourceClassName, true, getClass().getClassLoader()); 101 xads = (StandardXADataSource)datasourceClass.newInstance(); 102 try { 103 xads.setDriverName(driver); 104 xads.setUrl(url); 105 xads.setUser(login); 106 xads.setPassword(password); 107 } catch (Exception e) { 108 e.printStackTrace(); 109 throw e; 110 } 111 spds.setTransactionManager(jotm.getTransactionManager()); 112 113 spds.setDataSource(xads); 115 } 116 117 public int getValue() { 118 try { 119 boolean toClose = false; 122 if (conn.isClosed()) { 123 toClose = true; 124 conn = spds.getConnection(login, password); 125 126 } 127 128 Statement st = conn.createStatement(); 129 ResultSet rset = st.executeQuery(SQL_REQUEST); 130 int numcols = rset.getMetaData().getColumnCount(); 131 int res; 132 rset.next(); 133 res = Integer.parseInt(rset.getString(2)); 134 rset.close(); 135 st.close(); 136 137 if (toClose) 140 conn.close(); 141 return res; 142 } catch (Exception e) { 143 e.printStackTrace(); 144 } 145 return 0; 146 } 147 148 protected void tearDown() throws Exception { 149 try { 151 } catch (Exception e) { 154 throw e; 155 } 156 jotm.stop(); 157 jotm = null; 158 spds.stopPool(); 159 xads = null; 160 spds = null; 161 162 } 163 164 } 165 | Popular Tags |