
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class CrashTest {

	static String[] SERVERS = {
		"jdbc:mysql://sqlserver.com:3306/test?user=user&password=password",
		"jdbc:mysql://sqlserver.com:3306/test?user=user&password=password",
		"jdbc:mysql://sqlserver.com:3306/test?user=user&password=password"
	};

	static int table_num=500;


	private static synchronized String getNextTabName() {
		return "tab_" + table_num++;
	}


	static Connection getConnection() {

		Connection con=null;

		double randomMultiplier = Math.random();

		int randomNum = (int) (SERVERS.length * randomMultiplier);

		String connectString = SERVERS[randomNum];



		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance(); 
			con = 
				DriverManager.getConnection(connectString);

			con.setAutoCommit(true);

		} catch (SQLException ex) {
			// handle any errors
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return con;
	}

	static void testCreate(Connection con) {
		PreparedStatement ps = null;
		int  rs = 0;

		String key = null;
		try {
			String tablespace="ts_1";



			String tableName=getNextTabName();
			System.out.println("Creating : "  + tableName);
			ps = con.prepareStatement( "CREATE TABLE " + tableName + " (" +
					" row_key  VARCHAR(10), " +
					" data	   TEXT NOT NULL," +
					" PRIMARY KEY (row_key) )  TABLESPACE " + tablespace + " STORAGE DISK  ENGINE NDB");

			rs = ps.executeUpdate();


		} catch (Exception e) {
			System.out.println("testCreate Error:" +e.getMessage());
		} finally {

		}
	}


	public static void main(String[] args) throws Exception { 


		for(int numThreads = 3; numThreads > 0; numThreads--) {

			Thread[] threads = new Thread[numThreads];
			long t0 = System.currentTimeMillis();

			for (int i=0; i<threads.length; i++) { 
				final int x=i+1;
				threads[i] = new Thread(new Runnable() {
					public void run() {
						try {
							for (int numTables=0; numTables < 20; numTables++) {
								Connection con = getConnection();
								testCreate(con);
							}
						}catch( Exception e){
							System.out.println("t" + e);

						}
					}
				});
				threads[i].start();
			}

			for (int i=0; i<threads.length; i++) { 
				threads[i].join();
			}


			break;
		}
	}



}
