--- /dev/null
+package edu.tamu.aser.rvtest.derby;\r
+\r
+import org.apache.derby.jdbc.EmbeddedDriver;\r
+import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
+\r
+import edu.tamu.aser.reexecution.JUnit4MCRRunner;\r
+\r
+//10.3.2.1\r
+/**\r
+ * This class tests the thread-safeness of the Derby database system, using the embedded driver. With the\r
+ * proper choice of main arguments, which are probably different for different machines, it will sometimes show an\r
+ * SQLException with the following output from printStackTrace(). It may need to be run more than once, as having\r
+ * leftover views existing from prior runs sometimes seems to have an effect.\r
+ * \r
+ * <pre>\r
+ * java.lang.NullPointerException\r
+ * at org.apache.derby.iapi.sql.dictionary.TableDescriptor.getObjectName(TableDescriptor.java:758)\r
+ * at org.apache.derby.impl.sql.depend.BasicDependencyManager.getPersistentProviderInfos(BasicDependencyManager.java:677)\r
+ * at org.apache.derby.impl.sql.compile.CreateViewNode.bindViewDefinition(CreateViewNode.java:287)\r
+ * at org.apache.derby.impl.sql.compile.CreateViewNode.bind(CreateViewNode.java:183)\r
+ * at org.apache.derby.impl.sql.GenericStatement.prepMinion(GenericStatement.java:345)\r
+ * at org.apache.derby.impl.sql.GenericStatement.prepare(GenericStatement.java:119)\r
+ * at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(GenericLanguageConnectionContext.java:745)\r
+ * at org.apache.derby.impl.jdbc.EmbedStatement.execute(EmbedStatement.java:568)\r
+ * at org.apache.derby.impl.jdbc.EmbedStatement.execute(EmbedStatement.java:517)\r
+ * at TestEmbeddedMultiThreading.executeStatement(TestEmbeddedMultiThreading.java:109)\r
+ * at TestEmbeddedMultiThreading.access$100(TestEmbeddedMultiThreading.java:10)\r
+ * at TestEmbeddedMultiThreading$ViewCreatorDropper.run(TestEmbeddedMultiThreading.java:173)\r
+ * at java.lang.Thread.run(Thread.java:534)\r
+ * Stop here.\r
+ * </pre>\r
+ */\r
+@RunWith(JUnit4MCRRunner.class)\r
+public class DerbyTest\r
+{\r
+ \r
+ /**\r
+ * Invoke the test, providing a number of threads and a number of iterations.\r
+ * @param s arguments to the function, must be two integers: number of thread and number of iterations\r
+ */\r
+ static public void main(String[] args)\r
+ {\r
+ long st,et;\r
+ st=System.currentTimeMillis();\r
+ \r
+ String[] s = new String[2];\r
+ s[0]="2";\r
+ s[1]="2";\r
+ if(args.length==2)\r
+ {\r
+ s[0]=args[0];\r
+ s[1]=args[1];\r
+ }\r
+ try\r
+ {\r
+ if (s.length < 2)\r
+ {\r
+ System.out.println("Usage: main NUMBER_OF_THREADS NUMBER_OF_ITERATIONS");\r
+ System.exit(-1);\r
+ }\r
+ setup();\r
+\r
+ doit(Integer.parseInt(s[0]), Integer.parseInt(s[1]));\r
+ }\r
+ catch (Throwable e)\r
+ {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ et=System.currentTimeMillis();\r
+ System.out.println(et-st); \r
+ }\r
+\r
+ /**\r
+ * Creates the test object, registering the Derby embedded driver. If the test database does not already exists,\r
+ * creates that Derby database.\r
+ * @throws ClassNotFoundException thrown if the driver class could not be found, probably due to classpath problems\r
+ */\r
+ private static void setup()\r
+ {\r
+ //Class.forName("org.apache.derby.jdbc.EmbeddedDriver");\r
+\r
+ EmbeddedDriver driver = new EmbeddedDriver();\r
+ \r
+ try\r
+ {\r
+ java.sql.DriverManager.getConnection("jdbc:derby:DERBY2861;create=true");\r
+ }\r
+ catch (java.sql.SQLException e)\r
+ {\r
+ System.out.println("Exception creating database...assuming already exists");\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Runs the thread-safeness test.\r
+ * @param numThreads the number of threads to spawn for the test\r
+ * @param numIterations the number of iterations to run each thread\r
+ * @throws java.sql.SQLException thrown if there is an SQL error setting up the test\r
+ */\r
+ private static void doit(int numThreads, int numIterations) throws java.sql.SQLException\r
+ {\r
+ try\r
+ {\r
+ executeStatement(getConnection(), "CREATE TABLE schemamain.SOURCETABLE (col1 int, col2 char(10), col3 varchar(20), col4 decimal(10,5))");\r
+ }\r
+ catch (java.sql.SQLException e)\r
+ {\r
+ // Just report this...probably the table or view already exists\r
+ //System.out.println(e.getMessage());\r
+ }\r
+ try\r
+ {\r
+ executeStatement(getConnection(), "CREATE VIEW viewSource AS SELECT col1, col2 FROM schemamain.SOURCETABLE");\r
+ }catch(Exception e){}\r
+// \r
+ \r
+ Thread[] threads = new Thread[numThreads];\r
+ for (int i = 0; i < numThreads; i++)\r
+ {\r
+ threads[i] = new Thread(new ViewCreatorDropper("schema1.VIEW" + i, "viewSource", "*", numIterations));\r
+ threads[i].setName("thread-"+i);\r
+ }\r
+ for (int i = 0; i < numThreads; i++)\r
+ {\r
+ threads[i].start();\r
+ }\r
+ try {\r
+ for (int i = 0; i < numThreads; i++)\r
+ {\r
+ threads[i].join();\r
+ }\r
+ } catch (InterruptedException e) {\r
+ "reCrash_with".equals(e);\r
+ e.printStackTrace();\r
+ }\r
+\r
+ }\r
+\r
+ /**\r
+ * Returns a new connection to the test database\r
+ * @return a newly create connection\r
+ * @throws java.sql.SQLException thrown if the connection cannot be created\r
+ */\r
+ private static java.sql.Connection getConnection() throws java.sql.SQLException\r
+ {\r
+ return java.sql.DriverManager.getConnection("jdbc:derby:DERBY2861");\r
+ }\r
+\r
+ /**\r
+ * Creates and executes a new SQL statement on the connection, ensuring that the statement is closed, regardless\r
+ * of whether the statement execution throws an exception\r
+ * @param conn the connection against which to run the statement\r
+ * @param sql the SQL to execute\r
+ * @throws java.sql.SQLException thrown if there is any SQL error executing the statement (or creating it)\r
+ */\r
+ private static void executeStatement(java.sql.Connection conn, String sql) throws java.sql.SQLException\r
+ {\r
+ //System.out.println("" + Thread.currentThread() + " executing " + sql);\r
+ java.sql.Statement stmt = null;\r
+ try\r
+ {\r
+ stmt = conn.createStatement();\r
+ stmt.execute(sql);\r
+ }\r
+ finally\r
+ {\r
+ if (stmt != null)\r
+ {\r
+ try\r
+ {\r
+ stmt.close();\r
+ }\r
+ catch (java.sql.SQLException e)\r
+ {\r
+ System.out.println("Eating close() exception: " + e.getMessage());\r
+ }\r
+ }\r
+ }\r
+ }\r
+ \r
+ @Test\r
+ public void test() throws InterruptedException {\r
+ DerbyTest.main(new String[]{});\r
+ }\r
+\r
+}\r