/** * Creates multiple concurrent threads to test/demo concurrency issues. * * @author Terry Sergeant * @version for DS * *
In This program we are solving a simple problem of creating the behavior * of an autonumbering field. Although Postgres provides an elegant and * efficient system for solving this problem, we are temporarily assuming that * no such mechanism exists and we must solve it outselves.
* *Our solution involves issuing a query to determine the largest id number * that appears so far. We then add 1 to that value and use it as the next * id number to be used. We investigate what happens if multiple threads attempt * to do this action at approximately the same time.
*/ import java.sql.*; public class Transaction3 { public static void main(String [] args) throws Exception { final int NUM_CONNECTIONS = 10; CodeTimer timer= new CodeTimer(); Thread [] spool= new Thread[NUM_CONNECTIONS]; Connection con; Statement stmt; int i; timer.start(); con= DBHelper.connect("dbconnect.txt"); stmt= con.createStatement(); stmt.executeUpdate("DELETE FROM transactions.names"); con.close(); // create the threads for (i=0; i