--- experimental/server/DBInterface/DBLastInsertTest.java 2000/12/05 08:25:33 1.1 +++ experimental/server/DBInterface/DBLastInsertTest.java 2000/12/05 12:12:03 1.2 @@ -9,14 +9,14 @@ import java.sql.*; * with the same connection. * * CONCLUSION: This works on raptor. It is probably the method - * we shall use in the final database logging system. + * we shall use in the final database logging system. * * IMPORTANT: This method obtains the database user and database * connection password from the Password class. As such, this * class may be safely placed in a public CVS repository. * * @author $Author: pjm2 $ - * @version $Id: DBLastInsertTest.java,v 1.1 2000/12/05 08:25:33 pjm2 Exp $ + * @version $Id: DBLastInsertTest.java,v 1.2 2000/12/05 12:12:03 pjm2 Exp $ */ class DBLastInsertTest { @@ -37,19 +37,27 @@ class DBLastInsertTest { // Create a statement object. Statement stmt = conn.createStatement(); - // Lock all three test tables. (Waits until it can) - stmt.executeUpdate("LOCK TABLES packet WRITE, cpu WRITE, disk WRITE;"); + // Lock all two test tables. (Waits until it can) + // No other process will be able to use these tables + // until they are unlocked. + stmt.executeUpdate("LOCK TABLES packet_test WRITE, parameter_test WRITE;"); // Execute an insert statement. - stmt.executeUpdate("INSERT INTO packet (ip, seq_no, date, machine_name) VALUES ('0.0.0.0', 4543, 1423123, 'raptor');"); + stmt.executeUpdate("INSERT INTO packet_test (ip, machine_name, seq_no, sent_date, receipt_date) VALUES ('0.0.0.0', 'raptor', 1234, 12343, 12343);"); - // Wait 30 seconds. No other process should be able to use the - // tables in this time period. - Thread.sleep(30000); + // Get the last used insertion id for THIS connection ;-) + ResultSet rs = stmt.executeQuery("SELECT LAST_INSERT_ID();"); + rs.next(); + String packet_id = rs.getString("LAST_INSERT_ID()"); - // insert another one with the same id as above, but in the seq_no field. - stmt.executeUpdate("INSERT INTO packet (ip, seq_no, date, machine_name) VALUES ('0.0.0.0', LAST_INSERT_ID(), 1423123, 'raptor');"); + // insert some entries into the parameter_test table using + // the id number from the packet_test insertion. + stmt.executeUpdate("INSERT INTO parameter_test (packet_id, name, value) VALUES ("+packet_id+", 'packet.attributes.machine_name', 'raptor!');"); + stmt.executeUpdate("INSERT INTO parameter_test (packet_id, name, value) VALUES ("+packet_id+", 'packet.attributes.machine_name', 'raptor!');"); + stmt.executeUpdate("INSERT INTO parameter_test (packet_id, name, value) VALUES ("+packet_id+", 'packet.attributes.machine_name', 'raptor!');"); + stmt.executeUpdate("INSERT INTO parameter_test (packet_id, name, value) VALUES ("+packet_id+", 'packet.attributes.machine_name', 'raptor!');"); + // Remove the locks. stmt.executeUpdate("UNLOCK TABLES;"); // Close the statement and database connection.