/* JFox, the OpenSource J2EE Application Server
 *
 * Copyright (C) 2002 huihoo.com
 * Distributable under GNU LGPL license
 * See the GNU Lesser General Public License for more details.
 */

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.xa.TxDataSource;
import org.jfox.jdbc.xa.XADataSourceManager;
import org.jfox.jdbc.LoggablePreparedStatement;
import org.jfox.tm.TxManager;

/**
 * 
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class DB2TxDataSourceTest extends TestCase {

  private TxDataSource txds = null;

  protected void setUp() throws Exception {
    txds = XADataSourceManager.newDataSource("jdbc:db2://localhost;databaseName=SAMPLE","db2admin","db2admin");
    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 YANGYONG.SALES values('2003-09-02','YangYong','Beijing',1)");
      tm.commit();
      txds.destroy();
    }
    catch(Exception e){
      e.printStackTrace();
      fail(e.getMessage());
    }
  }

  public void testPreparedCommit() {
    System.out.println("########### testPreparedCommit ##########");
    try {
//      OracleLog.setLogStream(System.out);
      TransactionManager tm = TxManager.getInstance();
      tm.begin();
      Connection conn = txds.getConnection();
      PreparedStatement pstm = conn.prepareStatement("insert into YANGYONG.SALES values('2003-09-02','YangYong','Beijing',?)");

      for(int i=0; i<3; i++){
        pstm.setInt(1,i);
        System.out.println("--> QueryString: " + ((LoggablePreparedStatement)pstm).getSQLString());
        pstm.executeUpdate();
        Thread.sleep(500);
      }
      tm.commit();

      // re-get the connection from TxDataSource
      tm.begin();
      conn = txds.getConnection();
      pstm = conn.prepareStatement("insert into YANGYONG.SALES values('2003-09-02','YangYong','Beijing',?)");

      for(int i=0; i<3; i++){
        pstm.setInt(1,i);
        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 void testNotTransaction(){
    System.out.println("########### testNotTransaction ##########");
    try {
      Connection conn = txds.getConnection();
      conn.setAutoCommit(false);
      Statement stm = conn.createStatement();
      stm.executeUpdate("insert into person values(1,'No_Transaction')");
      conn.commit();
      txds.destroy();
    }
    catch(Exception e){
      e.printStackTrace();
      fail(e.getMessage());
    }
  }

*/
  public static void main(String[] args) {

  }
}