]> source.dussan.org Git - vaadin-framework.git/commitdiff
Reservations refreshed better.
authorMarc Englund <marc.englund@itmill.com>
Mon, 10 Dec 2007 14:08:22 +0000 (14:08 +0000)
committerMarc Englund <marc.englund@itmill.com>
Mon, 10 Dec 2007 14:08:22 +0000 (14:08 +0000)
Tried to fix hsqldb threading/locking issue: failed thus far...

svn changeset:3204/svn branch:trunk

src/com/itmill/toolkit/demo/reservation/ReservationApplication.java
src/com/itmill/toolkit/demo/reservation/SampleDB.java

index 130768aa7f41117d8ddad0c3bad4592bad1e737f..13a55b4a6030b2c477229aa3488576194c58b272 100644 (file)
@@ -57,13 +57,10 @@ public class ReservationApplication extends Application {
     private Label popupMessage;\r
 \r
     public void init() {\r
+        //\r
+        db = new SampleDB();\r
 \r
-        db = new SampleDB(true);\r
-        db.generateResources();\r
-        db.generateDemoUser();\r
-        db.generateReservations();\r
-\r
-        final Window mainWindow = new Window("Reservr");\r
+        final Window mainWindow = new Window("Reservr ");\r
         setMainWindow(mainWindow);\r
         setTheme("reservr");\r
 \r
@@ -272,7 +269,7 @@ public class ReservationApplication extends Application {
             getMainWindow().showNotification("Not available",\r
                     "Please choose another resource or time period.",\r
                     Notification.TYPE_HUMANIZED_MESSAGE);\r
-\r
+            refreshReservations(false);\r
             return;\r
         }\r
         map.clear();\r
index 0f3163c147c457f56eb278e7649a6bddfadbea1a..582c111866566ad9e61105b3ec63fc20a4063dc0 100644 (file)
@@ -95,31 +95,18 @@ public class SampleDB {
             + "), FOREIGN KEY (" + Reservation.PROPERTY_ID_RESERVED_BY_ID
             + ") REFERENCES " + User.TABLE + "(" + User.PROPERTY_ID_ID + "))";
 
-    private Connection connection = null;
+    private static Connection connection = null;
 
     /**
      * Create database.
      */
     public SampleDB() {
-        this(false);
-    }
-
-    public SampleDB(boolean recreate) {
         // connect to SQL database
         connect();
 
-        if (recreate) {
-            dropTables();
-        }
-
-        // initialize SQL database
-        createTables();
-
-        // test by executing sample JDBC query
-        testDatabase();
     }
 
-    private void dropTables() {
+    private synchronized void dropTables() {
         try {
             update("DROP TABLE " + Reservation.TABLE);
         } catch (final SQLException IGNORED) {
@@ -142,12 +129,26 @@ public class SampleDB {
      * named database in implicitly created into system memory.
      * 
      */
-    private void connect() {
-        try {
-            Class.forName("org.hsqldb.jdbcDriver").newInstance();
-            connection = DriverManager.getConnection(DB_URL);
-        } catch (final Exception e) {
-            throw new RuntimeException(e);
+    private synchronized void connect() {
+        if (connection == null) {
+            try {
+                Class.forName("org.hsqldb.jdbcDriver").newInstance();
+                connection = DriverManager.getConnection(DB_URL);
+            } catch (final Exception e) {
+                throw new RuntimeException(e);
+            }
+
+            dropTables();
+            // initialize SQL database
+            createTables();
+
+            // test by executing sample JDBC query
+            testDatabase();
+
+            generateResources();
+            generateDemoUser();
+            generateReservations();
+
         }
     }
 
@@ -157,7 +158,7 @@ public class SampleDB {
      * @param expression
      * @throws SQLException
      */
-    private void update(String expression) throws SQLException {
+    private synchronized void update(String expression) throws SQLException {
         Statement st = null;
         st = connection.createStatement();
         final int i = st.executeUpdate(expression);
@@ -172,7 +173,7 @@ public class SampleDB {
      * names as HSQLDB returns column names in capitalized form with this demo.
      * 
      */
-    private void createTables() {
+    private synchronized void createTables() {
         try {
             String stmt = null;
             stmt = CREATE_TABLE_RESOURCE;
@@ -206,7 +207,7 @@ public class SampleDB {
      * Test database connection with simple SELECT command.
      * 
      */
-    private String testDatabase() {
+    private synchronized String testDatabase() {
         String result = null;
         try {
             final Statement stmt = connection.createStatement(
@@ -296,7 +297,7 @@ public class SampleDB {
         }
     }
 
-    public void addReservation(Item resource, int reservedById,
+    public synchronized void addReservation(Item resource, int reservedById,
             Date reservedFrom, Date reservedTo, String description) {
         if (reservedFrom.after(reservedTo)) {
             final Date tmp = reservedTo;
@@ -382,7 +383,7 @@ public class SampleDB {
         }
     }
 
-    public void generateReservations() {
+    public synchronized void generateReservations() {
         final int days = 30;
         final String descriptions[] = { "Picking up guests from airport",
                 "Sightseeing with the guests",
@@ -427,7 +428,7 @@ public class SampleDB {
 
     }
 
-    public void generateResources() {
+    public synchronized void generateResources() {
 
         final Object[][] resources = {
                 // Turku
@@ -533,7 +534,7 @@ public class SampleDB {
         }
     }
 
-    public void generateDemoUser() {
+    public synchronized void generateDemoUser() {
         final String q = "INSERT INTO USER (" + User.PROPERTY_ID_FULLNAME + ","
                 + User.PROPERTY_ID_EMAIL + "," + User.PROPERTY_ID_PASSWORD
                 + ") VALUES (?,?,?)";