package org.jfox.jdbc.xa.test;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import javax.transaction.TransactionManager;
import junit.framework.TestCase;
import org.jfox.jdbc.LoggablePreparedStatement;
import org.jfox.jdbc.xa.TxDataSource;
import org.jfox.jdbc.xa.XADataSourceManager;
import org.jfox.tm.TxManager;
public class OracleTxDataSourceTest extends TestCase {
private TxDataSource txds = null;
protected void setUp() throws Exception {
txds = XADataSourceManager.newDataSource("jdbc:oracle:thin:@localhost:1521:yang","scott","tiger");
txds.setInitNum(3);
txds.init();
}
public void testSimple(){
try {
txds.getConnection();
txds.destroy();
}
catch(Exception e){
e.printStackTrace();
fail(e.getMessage());
}
}
public void testCommit() {
System.out.println("########### testCommit ##########");
try {
TransactionManager tm = TxManager.getInstance();
tm.begin();
Connection conn = txds.getConnection();
Statement stm = conn.createStatement();
stm.executeUpdate("insert into person values(" + System.currentTimeMillis() + ",'testCommit')");
tm.commit();
txds.destroy();
}
catch(Exception e){
e.printStackTrace();
fail(e.getMessage());
}
}
public void testPreparedCommit() {
System.out.println("########### testPreparedCommit ##########");
try {
TransactionManager tm = TxManager.getInstance();
tm.begin();
Connection conn = txds.getConnection();
PreparedStatement pstm = conn.prepareStatement("insert into person values(?,'testPreparedCommit')");
for(int i=0; i<3; i++){
pstm.setLong(1,System.currentTimeMillis());
System.out.println("--> QueryString: " + ((LoggablePreparedStatement)pstm).getSQLString());
pstm.executeUpdate();
Thread.sleep(500);
}
tm.commit();
tm.begin();
conn = txds.getConnection();
pstm = conn.prepareStatement("insert into person values(?,'testPreparedCommit')");
for(int i=0; i<3; i++){
pstm.setLong(1,System.currentTimeMillis());
System.out.println("--> QueryString: " + ((LoggablePreparedStatement)pstm).getSQLString());
pstm.executeUpdate();
Thread.sleep(500);
}
tm.commit();
txds.destroy();
}
catch(Exception e){
e.printStackTrace();
fail(e.getMessage());
}
}
public static void main(String[] args) {
}
}