package testsuite.simple;

import testsuite.BaseTestCase;
import java.lang.Runnable;
import java.sql.SQLException;


public class TestBug24721 extends BaseTestCase {

	class CloseThread implements Runnable{
		   Thread t;
		   CloseThread () {
		      t = new Thread(this,"Close thread");
		      t.start();
		   }
		   public void run() {
		      System.out.println("*Close* thread started");
		      try {
		    	  Thread.sleep(1500);
		      } catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
		      }
		      try {
		    	    System.out.println("*Close* thread, about to do stmt.execute");  
		    	  	stmt.execute("USE test");
		    	    System.out.println("*Close* thread, stmt.execute done");  
					
		    	    System.out.println("*Close* thread, about to do stmt.close");  
		    	    stmt.close();
		    	    System.out.println("*Close* thread, stmt.close done");  
			   } catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
			   }
		      System.out.println("*Close* thread terminated");
		   }
		}

	class CancelThread implements Runnable{
		   Thread t;
		   CancelThread () {
		      t = new Thread(this,"Cancel thread");
		      t.start();
		   }
		   public void run() {
		      System.out.println("*Cancel* thread started");
		      try {
				Thread.sleep(1500);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}		      
		      try {
		    	  System.out.println("*Cancel* thread, about to do stmt.cancel()");  
		    	  stmt.cancel();
		    	  System.out.println("*Cancel* thread, stmt.cancel() done");  
		      } catch (SQLException e) {
				// TODO Auto-generated catch block
		    	  e.printStackTrace();
		      }
		      System.out.println("*Cancel* thread terminated");
		   }
		}

	public TestBug24721(String name) {
		// TODO Auto-generated constructor stub
		super(name);
	}

	public void testBug24065() throws Exception {
		
		CloseThread thr1 = new CloseThread();
		CancelThread thr2 = new CancelThread();		
		
		 try {
	         System.out.println("Threads Joining.");
	         thr1.t.join();
	         thr2.t.join();
	      } catch (InterruptedException e) {
	        System.out.println(
	                  "Exception: Thread main interrupted.");
	      }		
//		if (versionMeetsMinimum(5, 0)) {
//			this.stmt.executeUpdate("DROP PROCEDURE IF EXISTS testBug24065");
//			
//			createTable("tblTestBug24065", "("
//					+ "id varchar(20) NOT NULL default '',"
//					+ "Income double(19,2) default NULL)");	
//			
//			this.stmt.executeUpdate("CREATE PROCEDURE testBug24065()"
//					+ "BEGIN"
//					+ "\n SELECT * FROM tblTestBug24065;" 
//					+ "\nEND");
//
//			this.stmt.executeUpdate("INSERT INTO tblTestBug24065 (`id`,`Income`) VALUES"
//					+ "('a',4094.00),"
//					+ "('b',500.00),"
//					+ "('c',3462.17),"
//					+ " ('d',500.00),"
//					+ " ('e',600.00)");
//			
//			try {
//				CallableStatement spUserUpdate = this.conn.prepareCall("{ CALL testBug24065() }");
//				this.rs =spUserUpdate.executeQuery();
//                ResultSetMetaData rsmd= this.rs.getMetaData();
//                int columnCount=rsmd.getColumnCount();
//                while(this.rs.next()) {
//                    for(int i=0;i<columnCount;i++) {
//                    	System.out.print(rsmd.getColumnName(i+1)+"="+rs.getObject(i+1));
//                        System.out.print('\t');
//                    }
//                    System.out.println();
//                }
//                spUserUpdate.close();                
//			} finally {
//				closeMemberJDBCResources();
//			}
//		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		junit.textui.TestRunner.run(TestBug24721.class);
	}

}
