--- /dev/null
+package derby2861;\r
+\r
+public class ViewCreatorDropper implements Runnable {\r
+ \r
+ /** The name of the view to create and drop */\r
+ String m_viewName;\r
+ /** The source (view) referenced by the created view */\r
+ String m_sourceName;\r
+ /** The SQL fragment specifying the columns to included in the created view */\r
+ String m_columns;\r
+ /** How many times to create/drop the view */\r
+ int m_iterations;\r
+\r
+ /**\r
+ * Constructs the runnable object for the test.\r
+ * @param viewName the name of the view to create and drop\r
+ * @param sourceName the source (view) referenced by the created view\r
+ * @param columns the SQL fragment specifying the columns to included in the created view\r
+ * @param iterations how many times to create/drop the view\r
+ * @throws java.sql.SQLException\r
+ */\r
+ public ViewCreatorDropper(String viewName, String sourceName, String columns, int iterations)\r
+ {\r
+ m_viewName = viewName;\r
+ m_sourceName = sourceName;\r
+ m_columns = columns;\r
+ m_iterations = iterations;\r
+ }\r
+\r
+ /**\r
+ * @see Thread#run()\r
+ */\r
+ public void run()\r
+ {\r
+ int i = 0;\r
+ try\r
+ {\r
+ java.sql.Connection conn = getConnection();\r
+ for (i = 0; i < m_iterations; i++)\r
+ {\r
+ assert conn != null;\r
+ //if (i % 5 == 0) System.out.println(" (" + Thread.currentThread() + " iteration " + i + ") ");\r
+ executeStatement(conn, "CREATE VIEW " + m_viewName + " AS SELECT " + m_columns + " FROM " + m_sourceName);\r
+ executeStatement(conn, "DROP VIEW " + m_viewName);\r
+ }\r
+ }\r
+ catch (java.sql.SQLException e)\r
+ {\r
+ e.printStackTrace();\r
+ \r
+ // Grab up all the error message in one string, to guard against output from different threads being\r
+ // interleaved in the console. (That might happen anyway, but so it goes.)\r
+// java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();\r
+// java.io.PrintStream p = new java.io.PrintStream(bos);\r
+// //p.println("" + Thread.currentThread() + " exception after " + i + "iterations:");\r
+// \r
+// e.printStackTrace(p);\r
+// p.flush();\r
+//\r
+// String msg = bos.toString();\r
+// System.err.print(msg);\r
+// "Cared_by_CLAP_msg".equals(msg);\r
+// // If we got the exception we were testing for, just quit.\r
+// if (msg.contains("java.lang.NullP"))\r
+// { \r
+// "reCrash_with".equals(e);\r
+// e.printStackTrace();\r
+// //System.err.println("Stop here.");\r
+// System.exit(-1);\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 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 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