View | Details | Raw Unified | Return to bug 25889
Collapse All | Expand All

(-)catalina/src/share/org/apache/catalina/session/JDBCStore.java (-396 / +453 lines)
Line 67 Link Here
67
import org.apache.catalina.Container;
68
import org.apache.catalina.LifecycleException;
69
import org.apache.catalina.Loader;
70
import org.apache.catalina.Session;
71
import org.apache.catalina.Store;
72
import org.apache.catalina.util.CustomObjectInputStream;
Line 76 Link Here
76
import java.sql.DriverManager;
82
import java.sql.Driver;
77
--
Lines 81-87 Link Here
81
87
import java.util.Properties;
82
import org.apache.catalina.Container;
83
import org.apache.catalina.LifecycleException;
84
import org.apache.catalina.Loader;
85
import org.apache.catalina.Session;
86
import org.apache.catalina.Store;
87
import org.apache.catalina.util.CustomObjectInputStream;
88
--
Line 99 Link Here
99
    extends StoreBase implements Store {
99
        extends StoreBase implements Store {
100
--
Line 122 Link Here
122
     * The connection username to use when trying to connect to the database.
123
     */
124
    protected String connectionName = null;
125
126
127
    /**
128
     * The connection URL to use when trying to connect to the database.
129
     */
130
    protected String connectionPassword = null;
131
132
    /**
Line 124 Link Here
124
    protected String connString = null;
135
    protected String connectionURL = null;
125
--
Line 129 Link Here
129
    private Connection conn = null;
140
    private Connection dbConnection = null;
130
--
141
142
    /**
143
     * Instance of the JDBC Driver class we use as a connection factory.
144
     */
145
    protected Driver driver = null;
Line 211 Link Here
211
        return(info);
227
        return (info);
212
--
Line 240 Link Here
240
        return(threadName);
256
        return (threadName);
241
--
Line 247 Link Here
247
        return(storeName);
263
        return (storeName);
248
--
Lines 259-260 Link Here
259
                                   oldDriverName,
275
                oldDriverName,
260
                                   this.driverName);
276
                this.driverName);
261
--
Line 268 Link Here
268
        return(this.driverName);
284
        return (this.driverName);
269
--
285
    }
286
287
    /**
288
     * Return the username to use to connect to the database.
289
     *
290
     */
291
    public String getConnectionName() {
292
        return connectionName;
293
    }
294
295
    /**
296
     * Set the username to use to connect to the database.
297
     *
298
     * @param connectionName Username
299
     */
300
    public void setConnectionName(String connectionName) {
301
        this.connectionName = connectionName;
302
    }
303
304
    /**
305
     * Return the password to use to connect to the database.
306
     *
307
     */
308
    public String getConnectionPassword() {
309
        return connectionPassword;
310
    }
311
312
    /**
313
     * Set the password to use to connect to the database.
314
     *
315
     * @param connectionPassword User password
316
     */
317
    public void setConnectionPassword(String connectionPassword) {
318
        this.connectionPassword = connectionPassword;
Lines 277-281 Link Here
277
        String oldConnString = this.connString;
327
        String oldConnString = this.connectionURL;
278
        this.connString = connectionURL;
328
        this.connectionURL = connectionURL;
279
        support.firePropertyChange("connString",
329
        support.firePropertyChange("connectionURL",
280
                                   oldConnString,
330
                oldConnString,
281
                                   this.connString);
331
                this.connectionURL);
282
--
Line 288 Link Here
288
        return(this.connString);
338
        return (this.connectionURL);
289
--
Lines 300-301 Link Here
300
                                   oldSessionTable,
350
                oldSessionTable,
301
                                   this.sessionTable);
351
                this.sessionTable);
302
--
Line 308 Link Here
308
        return(this.sessionTable);
358
        return (this.sessionTable);
309
--
Lines 320-321 Link Here
320
                                   oldSessionAppCol,
370
                oldSessionAppCol,
321
                                   this.sessionAppCol);
371
                this.sessionAppCol);
322
--
Line 328 Link Here
328
        return(this.sessionAppCol);
378
        return (this.sessionAppCol);
329
--
Lines 340-341 Link Here
340
                                   oldSessionIdCol,
390
                oldSessionIdCol,
341
                                   this.sessionIdCol);
391
                this.sessionIdCol);
342
--
Line 348 Link Here
348
        return(this.sessionIdCol);
398
        return (this.sessionIdCol);
349
--
Lines 360-361 Link Here
360
                                   oldSessionDataCol,
410
                oldSessionDataCol,
361
                                   this.sessionDataCol);
411
                this.sessionDataCol);
362
--
Line 368 Link Here
368
        return(this.sessionDataCol);
418
        return (this.sessionDataCol);
369
--
Lines 380-381 Link Here
380
                                   oldSessionValidCol,
430
                oldSessionValidCol,
381
                                   this.sessionValidCol);
431
                this.sessionValidCol);
382
--
Line 388 Link Here
388
        return(this.sessionValidCol);
438
        return (this.sessionValidCol);
389
--
Lines 400-401 Link Here
400
                                   oldSessionMaxInactiveCol,
450
                oldSessionMaxInactiveCol,
401
                                   this.sessionMaxInactiveCol);
451
                this.sessionMaxInactiveCol);
402
--
Line 408 Link Here
408
        return(this.sessionMaxInactiveCol);
458
        return (this.sessionMaxInactiveCol);
409
--
Lines 420-421 Link Here
420
                                   oldSessionLastAccessedCol,
470
                oldSessionLastAccessedCol,
421
                                   this.sessionLastAccessedCol);
471
                this.sessionLastAccessedCol);
422
--
Line 428 Link Here
428
        return(this.sessionLastAccessedCol);
478
        return (this.sessionLastAccessedCol);
429
--
Lines 442-443 Link Here
442
            "SELECT " + sessionIdCol + " FROM " + sessionTable +
492
                "SELECT " + sessionIdCol + " FROM " + sessionTable +
443
            " WHERE " + sessionAppCol + " = ?";
493
                " WHERE " + sessionAppCol + " = ?";
444
--
Lines 446-457 Link Here
446
        int i;
496
        synchronized (this) {
447
497
            int numberOfTries = 2;
448
        synchronized(this) {
498
            while (numberOfTries > 0) {
449
            Connection _conn = getConnection();
499
450
500
                Connection _conn = getConnection();
451
            if(_conn == null) {
501
                if (_conn == null) {
452
                return(new String[0]);
502
                    return (new String[0]);
453
            }
454
455
            try {
456
                if(preparedKeysSql == null) {
457
                    preparedKeysSql = _conn.prepareStatement(keysSql);
458
--
Lines 459-471 Link Here
459
460
                preparedKeysSql.setString(1, getName());
461
                rst = preparedKeysSql.executeQuery();
462
                ArrayList tmpkeys = new ArrayList();
463
                if (rst != null) {
464
                    while(rst.next()) {
465
                        tmpkeys.add(rst.getString(1));
466
                    }
467
                }
468
                keys = (String[]) tmpkeys.toArray(new String[tmpkeys.size()]);
469
            } catch(SQLException e) {
470
                log(sm.getString(getStoreName()+".SQLException", e));
471
            } finally {
Lines 473-474 Link Here
473
                    if(rst != null) {
505
                    if (preparedKeysSql == null) {
474
                        rst.close();
506
                        preparedKeysSql = _conn.prepareStatement(keysSql);
475
--
507
                    }
508
509
                    preparedKeysSql.setString(1, getName());
510
                    rst = preparedKeysSql.executeQuery();
511
                    ArrayList tmpkeys = new ArrayList();
512
                    if (rst != null) {
513
                        while (rst.next()) {
514
                            tmpkeys.add(rst.getString(1));
515
                        }
516
                    }
517
                    keys = (String[]) tmpkeys.toArray(new String[tmpkeys.size()]);
518
                } catch (SQLException e) {
519
                    log(sm.getString(getStoreName() + ".SQLException", e));
520
                    // Close the connection so that it gets reopened next time
521
                    if (dbConnection != null)
522
                        close(dbConnection);
523
                } finally {
524
                    try {
525
                        if (rst != null) {
526
                            rst.close();
527
                        }
528
                    } catch (SQLException e) {
529
                        ;
Lines 476-478 Link Here
476
                } catch(SQLException e) {
477
                    ;
478
                }
Line 480 Link Here
480
                release(_conn);
532
                    release(_conn);
481
--
533
                }
534
                numberOfTries--;
Line 484 Link Here
484
        return(keys);
538
        return (keys);
485
--
Lines 496-498 Link Here
496
        String sizeSql = 
550
        String sizeSql =
497
            "SELECT COUNT(" + sessionIdCol + ") FROM " + sessionTable +
551
                "SELECT COUNT(" + sessionIdCol + ") FROM " + sessionTable +
498
            " WHERE " + sessionAppCol + " = ?";
552
                " WHERE " + sessionAppCol + " = ?";
499
--
Lines 501-506 Link Here
501
        synchronized(this) {
555
        synchronized (this) {
502
            Connection _conn = getConnection();
556
            int numberOfTries = 2;
503
557
            while (numberOfTries > 0) {
504
            if(_conn == null) {
558
                Connection _conn = getConnection();
505
                return(size);
506
            }
507
--
Lines 508-510 Link Here
508
            try {
560
                if (_conn == null) {
509
                if(preparedSizeSql == null) {
561
                    return (size);
510
                    preparedSizeSql = _conn.prepareStatement(sizeSql);
511
--
Lines 513-520 Link Here
513
                preparedSizeSql.setString(1, getName());
514
                rst = preparedSizeSql.executeQuery();
515
                if (rst.next()) {
516
                    size = rst.getInt(1);
517
                }
518
            } catch(SQLException e) {
519
                log(sm.getString(getStoreName()+".SQLException", e));
520
            } finally {
Lines 522-526 Link Here
522
                    if(rst != null)
565
                    if (preparedSizeSql == null) {
523
                        rst.close();
566
                        preparedSizeSql = _conn.prepareStatement(sizeSql);
524
                } catch(SQLException e) {
567
                    }
525
                    ;
568
526
                }
569
                    preparedSizeSql.setString(1, getName());
527
--
570
                    rst = preparedSizeSql.executeQuery();
571
                    if (rst.next()) {
572
                        size = rst.getInt(1);
573
                    }
574
                } catch (SQLException e) {
575
                    log(sm.getString(getStoreName() + ".SQLException", e));
576
                    if (dbConnection != null)
577
                        close(dbConnection);
578
                } finally {
579
                    try {
580
                        if (rst != null)
581
                            rst.close();
582
                    } catch (SQLException e) {
583
                        ;
584
                    }
Line 528 Link Here
528
                release(_conn);
586
                    release(_conn);
529
--
587
                }
588
                numberOfTries--;
Line 531 Link Here
531
        return(size);
591
        return (size);
532
--
Line 544 Link Here
544
        throws ClassNotFoundException, IOException {
604
            throws ClassNotFoundException, IOException {
545
--
Lines 553-565 Link Here
553
            "SELECT " + sessionIdCol + ", " + sessionDataCol + " FROM " +
613
                "SELECT " + sessionIdCol + ", " + sessionDataCol + " FROM " +
554
            sessionTable + " WHERE " + sessionIdCol + " = ? AND " +
614
                sessionTable + " WHERE " + sessionIdCol + " = ? AND " +
555
            sessionAppCol + " = ?";
615
                sessionAppCol + " = ?";
556
616
557
        synchronized(this) {
617
        synchronized (this) {
558
            Connection _conn = getConnection();
618
            int numberOfTries = 2;
559
            if(_conn == null) {
619
            while (numberOfTries > 0) {
560
                return(null);
620
                Connection _conn = getConnection();
561
            }
621
                if (_conn == null) {
562
622
                    return (null);
563
            try {
564
                if(preparedLoadSql == null) {
565
                    preparedLoadSql = _conn.prepareStatement(loadSql);
566
--
Lines 568-589 Link Here
568
                preparedLoadSql.setString(1, id);
625
                try {
569
                preparedLoadSql.setString(2, getName());
626
                    if (preparedLoadSql == null) {
570
                rst = preparedLoadSql.executeQuery();
627
                        preparedLoadSql = _conn.prepareStatement(loadSql);
571
                if (rst.next()) {
572
                    bis = new BufferedInputStream(rst.getBinaryStream(2));
573
574
                    if (container != null) {
575
                        loader = container.getLoader();
576
                    }
577
                    if (loader != null) {
578
                        classLoader = loader.getClassLoader();
579
                    }
580
                    if (classLoader != null) {
581
                        ois = new CustomObjectInputStream(bis,
582
                                                          classLoader);
583
                    } else {
584
                        ois = new ObjectInputStream(bis);
585
                    }
586
587
                    if (debug > 0) {
588
                        log(sm.getString(getStoreName()+".loading",
589
                                         id, sessionTable));
590
--
Lines 592-594 Link Here
592
                    _session = (StandardSession) manager.createEmptySession();
630
                    preparedLoadSql.setString(1, id);
593
                    _session.readObjectData(ois);
631
                    preparedLoadSql.setString(2, getName());
594
                    _session.setManager(manager);
632
                    rst = preparedLoadSql.executeQuery();
595
--
633
                    if (rst.next()) {
634
                        bis = new BufferedInputStream(rst.getBinaryStream(2));
635
636
                        if (container != null) {
637
                            loader = container.getLoader();
638
                        }
639
                        if (loader != null) {
640
                            classLoader = loader.getClassLoader();
641
                        }
642
                        if (classLoader != null) {
643
                            ois = new CustomObjectInputStream(bis,
644
                                    classLoader);
645
                        } else {
646
                            ois = new ObjectInputStream(bis);
647
                        }
648
649
                        if (debug > 0) {
650
                            log(sm.getString(getStoreName() + ".loading",
651
                                    id, sessionTable));
652
                        }
653
654
                        _session = (StandardSession) manager.createEmptySession();
655
                        _session.readObjectData(ois);
656
                        _session.setManager(manager);
Lines 596-604 Link Here
596
                } else if (debug > 0) {
658
                    } else if (debug > 0) {
597
                    log(getStoreName()+": No persisted data object found");
659
                        log(getStoreName() + ": No persisted data object found");
598
                }
599
            } catch(SQLException e) {
600
                log(sm.getString(getStoreName()+".SQLException", e));
601
            } finally {
602
                try {
603
                    if(rst != null) {
604
                        rst.close();
605
--
Lines 606-609 Link Here
606
                } catch(SQLException e) {
661
                } catch (SQLException e) {
607
                    ;
662
                    log(sm.getString(getStoreName() + ".SQLException", e));
608
                }
663
                    if (dbConnection != null)
609
                if (ois != null) {
664
                        close(dbConnection);
610
--
665
                } finally {
Lines 611-612 Link Here
611
                        ois.close();
667
                        if (rst != null) {
612
                    } catch (IOException e) {
668
                            rst.close();
613
--
669
                        }
670
                    } catch (SQLException e) {
Line 615 Link Here
673
                    if (ois != null) {
674
                        try {
675
                            ois.close();
676
                        } catch (IOException e) {
677
                            ;
678
                        }
679
                    }
680
                    release(_conn);
Line 616 Link Here
616
                release(_conn);
682
                numberOfTries--;
617
--
Line 620 Link Here
620
        return(_session);
686
        return (_session);
621
--
Lines 634-638 Link Here
634
            "DELETE FROM " + sessionTable + " WHERE " + sessionIdCol +
700
                "DELETE FROM " + sessionTable + " WHERE " + sessionIdCol +
635
            " = ?  AND " + sessionAppCol + " = ?";
701
                " = ?  AND " + sessionAppCol + " = ?";
636
637
        synchronized(this) {
638
            Connection _conn = getConnection();
639
--
Lines 640-642 Link Here
640
            if(_conn == null) {
703
        synchronized (this) {
641
                return;
704
            int numberOfTries = 2;
642
            }
705
            while (numberOfTries > 0) {
643
--
706
                Connection _conn = getConnection();
Lines 644-646 Link Here
644
            try {
708
                if (_conn == null) {
645
                if(preparedRemoveSql == null) {
709
                    return;
646
                    preparedRemoveSql = _conn.prepareStatement(removeSql);
647
--
Lines 649-655 Link Here
649
                preparedRemoveSql.setString(1, id);
712
                try {
650
                preparedRemoveSql.setString(2, getName());
713
                    if (preparedRemoveSql == null) {
651
                preparedRemoveSql.execute();
714
                        preparedRemoveSql = _conn.prepareStatement(removeSql);
652
            } catch(SQLException e) {
715
                    }
653
                log(sm.getString(getStoreName()+".SQLException", e));
716
654
            } finally {
717
                    preparedRemoveSql.setString(1, id);
655
                release(_conn);
718
                    preparedRemoveSql.setString(2, getName());
656
--
719
                    preparedRemoveSql.execute();
720
                } catch (SQLException e) {
721
                    log(sm.getString(getStoreName() + ".SQLException", e));
722
                    if (dbConnection != null)
723
                        close(dbConnection);
724
                } finally {
725
                    release(_conn);
726
                }
727
                numberOfTries--;
Line 660 Link Here
660
            log(sm.getString(getStoreName()+".removing", id, sessionTable));
732
            log(sm.getString(getStoreName() + ".removing", id, sessionTable));
661
--
Line 671 Link Here
671
            "DELETE FROM " + sessionTable + " WHERE " + sessionAppCol + " = ?";
743
                "DELETE FROM " + sessionTable + " WHERE " + sessionAppCol + " = ?";
672
--
Lines 673-681 Link Here
673
        synchronized(this) {
745
        synchronized (this) {
674
            Connection _conn = getConnection();
746
            int numberOfTries = 2;
675
            if(_conn == null) {
747
            while (numberOfTries > 0) {
676
                return;
748
                Connection _conn = getConnection();
677
            }
749
                if (_conn == null) {
678
750
                    return;
679
            try {
680
                if(preparedClearSql == null) {
681
                    preparedClearSql = _conn.prepareStatement(clearSql);
682
--
Lines 684-689 Link Here
684
                preparedClearSql.setString(1, getName());
753
                try {
685
                preparedClearSql.execute();
754
                    if (preparedClearSql == null) {
686
            } catch(SQLException e) {
755
                        preparedClearSql = _conn.prepareStatement(clearSql);
687
                log(sm.getString(getStoreName()+".SQLException", e));
756
                    }
688
            } finally {
757
689
                release(_conn);
758
                    preparedClearSql.setString(1, getName());
690
--
759
                    preparedClearSql.execute();
760
                } catch (SQLException e) {
761
                    log(sm.getString(getStoreName() + ".SQLException", e));
762
                    if (dbConnection != null)
763
                        close(dbConnection);
764
                } finally {
765
                    release(_conn);
766
                }
767
                numberOfTries--;
Lines 702-707 Link Here
702
            "INSERT INTO " + sessionTable + " (" + sessionIdCol + ", " +
780
                "INSERT INTO " + sessionTable + " (" + sessionIdCol + ", " +
703
            sessionAppCol + ", " +
781
                sessionAppCol + ", " +
704
            sessionDataCol + ", " +
782
                sessionDataCol + ", " +
705
            sessionValidCol + ", " +
783
                sessionValidCol + ", " +
706
            sessionMaxInactiveCol + ", " +
784
                sessionMaxInactiveCol + ", " +
707
            sessionLastAccessedCol + ") VALUES (?, ?, ?, ?, ?, ?)";
785
                sessionLastAccessedCol + ") VALUES (?, ?, ?, ?, ?, ?)";
708
--
Lines 713-717 Link Here
713
        synchronized(this) {
791
        synchronized (this) {
714
            Connection _conn = getConnection();
792
            int numberOfTries = 2;
715
            if(_conn == null) {
793
            while (numberOfTries > 0) {
716
                return;
794
                Connection _conn = getConnection();
717
            }
795
                if (_conn == null) {
718
--
796
                    return;
797
                }
Lines 719-722 Link Here
719
            // If sessions already exist in DB, remove and insert again.
799
                // If sessions already exist in DB, remove and insert again.
720
            // TODO:
800
                // TODO:
721
            // * Check if ID exists in database and if so use UPDATE.
801
                // * Check if ID exists in database and if so use UPDATE.
722
            remove(session.getId());
802
                remove(session.getId());
723
--
Lines 724-726 Link Here
724
            try {
804
                try {
725
                bos = new ByteArrayOutputStream();
805
                    bos = new ByteArrayOutputStream();
726
                oos = new ObjectOutputStream(new BufferedOutputStream(bos));
806
                    oos = new ObjectOutputStream(new BufferedOutputStream(bos));
727
--
Lines 728-729 Link Here
728
                ((StandardSession)session).writeObjectData(oos);
808
                    ((StandardSession) session).writeObjectData(oos);
729
                oos.close();
809
                    oos.close();
730
--
810
                    oos = null;
811
                    byte[] obs = bos.toByteArray();
812
                    int size = obs.length;
813
                    bis = new ByteArrayInputStream(obs, 0, size);
814
                    in = new BufferedInputStream(bis, size);
Lines 731-757 Link Here
731
                byte[] obs = bos.toByteArray();
816
                    if (preparedSaveSql == null) {
732
                int size = obs.length;
817
                        preparedSaveSql = _conn.prepareStatement(saveSql);
733
                bis = new ByteArrayInputStream(obs, 0, size);
818
                    }
734
                in = new BufferedInputStream(bis, size);
735
736
                if(preparedSaveSql == null) {
737
                    preparedSaveSql = _conn.prepareStatement(saveSql);
738
                }
739
740
                preparedSaveSql.setString(1, session.getId());
741
                preparedSaveSql.setString(2, getName());
742
                preparedSaveSql.setBinaryStream(3, in, size);
743
                preparedSaveSql.setString(4, session.isValid()?"1":"0");
744
                preparedSaveSql.setInt(5, session.getMaxInactiveInterval());
745
                preparedSaveSql.setLong(6, session.getLastAccessedTime());
746
                preparedSaveSql.execute();
747
            } catch(SQLException e) {
748
                log(sm.getString(getStoreName()+".SQLException", e));
749
            } catch (IOException e) {
750
                ;
751
            } finally {
752
                if(bis != null) {
753
                    bis.close();
754
                }
755
                if(in != null) {
756
                    in.close();
757
                }
758
--
Line 759 Link Here
759
                release(_conn);
820
                    preparedSaveSql.setString(1, session.getId());
760
--
821
                    preparedSaveSql.setString(2, getName());
822
                    preparedSaveSql.setBinaryStream(3, in, size);
823
                    preparedSaveSql.setString(4, session.isValid() ? "1" : "0");
824
                    preparedSaveSql.setInt(5, session.getMaxInactiveInterval());
825
                    preparedSaveSql.setLong(6, session.getLastAccessedTime());
826
                    preparedSaveSql.execute();
827
                } catch (SQLException e) {
828
                    log(sm.getString(getStoreName() + ".SQLException", e));
829
                    if (dbConnection != null)
830
                        close(dbConnection);
831
                } catch (IOException e) {
832
                    ;
833
                } finally {
834
                    if (oos != null) {
835
                        oos.close();
836
                    }
837
                    if (bis != null) {
838
                        bis.close();
839
                    }
840
                    if (in != null) {
841
                        in.close();
842
                    }
843
844
                    release(_conn);
845
                }
846
                numberOfTries--;
Lines 764-765 Link Here
764
            log(sm.getString(getStoreName()+".saving",
851
            log(sm.getString(getStoreName() + ".saving",
765
                             session.getId(), sessionTable));
852
                    session.getId(), sessionTable));
766
--
Line 778 Link Here
778
    protected Connection getConnection(){
865
    protected Connection getConnection() {
779
--
Lines 780-787 Link Here
780
            if(conn == null || conn.isClosed()) {
867
            if (dbConnection == null || dbConnection.isClosed()) {
781
                Class.forName(driverName);
868
                log(sm.getString(getStoreName() + ".checkConnectionDBClosed"));
782
                log(sm.getString(getStoreName()+".checkConnectionDBClosed"));
869
                open();
783
                conn = DriverManager.getConnection(connString);
870
                if (dbConnection == null || dbConnection.isClosed()) {
784
                conn.setAutoCommit(true);
871
                    log(sm.getString(getStoreName() + ".checkConnectionDBReOpenFail"));
785
786
                if(conn == null || conn.isClosed()) {
787
                    log(sm.getString(getStoreName()+".checkConnectionDBReOpenFail"));
788
--
Lines 790-795 Link Here
790
        } catch (SQLException ex){
874
        } catch (SQLException ex) {
791
            log(sm.getString(getStoreName()+".checkConnectionSQLException",
875
            log(sm.getString(getStoreName() + ".checkConnectionSQLException",
792
                             ex.toString()));
876
                    ex.toString()));
793
        } catch (ClassNotFoundException ex) {
877
        }
794
            log(sm.getString(getStoreName()+".checkConnectionClassNotFoundException",
878
795
                             ex.toString()));
879
        return dbConnection;
796
--
880
    }
881
882
    /**
883
     * Open (if necessary) and return a database connection for use by
884
     * this Realm.
885
     *
886
     * @exception SQLException if a database error occurs
887
     */
888
    protected Connection open() throws SQLException {
889
890
        // Do nothing if there is a database connection already open
891
        if (dbConnection != null)
892
            return (dbConnection);
893
894
        // Instantiate our database driver if necessary
895
        if (driver == null) {
896
            try {
897
                Class clazz = Class.forName(driverName);
898
                driver = (Driver) clazz.newInstance();
899
            } catch (ClassNotFoundException ex) {
900
                log(sm.getString(getStoreName() + ".checkConnectionClassNotFoundException",
901
                        ex.toString()));
902
            } catch (InstantiationException ex) {
903
                log(sm.getString(getStoreName() + ".checkConnectionClassNotFoundException",
904
                        ex.toString()));
905
            } catch (IllegalAccessException ex) {
906
                log(sm.getString(getStoreName() + ".checkConnectionClassNotFoundException",
907
                        ex.toString()));
908
            }
909
        }
910
911
        // Open a new connection
912
        Properties props = new Properties();
913
        if (connectionName != null)
914
            props.put("user", connectionName);
915
        if (connectionPassword != null)
916
            props.put("password", connectionPassword);
917
        dbConnection = driver.connect(connectionURL, props);
918
        dbConnection.setAutoCommit(true);
919
        return (dbConnection);
920
921
    }
922
923
    /**
924
     * Close the specified database connection.
925
     *
926
     * @param dbConnection The connection to be closed
927
     */
928
    protected void close(Connection dbConnection) {
929
930
        // Do nothing if the database connection is already closed
931
        if (dbConnection == null)
932
            return;
933
934
        // Close our prepared statements (if any)
935
        try {
936
            preparedSizeSql.close();
937
        } catch (Throwable f) {
938
            ;
939
        }
940
        this.preparedSizeSql = null;
941
942
        try {
943
            preparedKeysSql.close();
944
        } catch (Throwable f) {
945
            ;
946
        }
947
        this.preparedKeysSql = null;
948
949
        try {
950
            preparedSaveSql.close();
951
        } catch (Throwable f) {
952
            ;
953
        }
954
        this.preparedSaveSql = null;
955
956
        try {
957
            preparedClearSql.close();
958
        } catch (Throwable f) {
959
            ;
960
        }
961
        this.preparedClearSql = null;
962
963
        try {
964
            preparedRemoveSql.close();
965
        } catch (Throwable f) {
966
            ;
967
        }
968
        this.preparedRemoveSql = null;
969
970
        try {
971
            preparedLoadSql.close();
972
        } catch (Throwable f) {
973
            ;
974
        }
975
        this.preparedLoadSql = null;
976
977
        // Close this database connection, and log any errors
978
        try {
979
            dbConnection.close();
980
        } catch (SQLException e) {
981
            log(sm.getString(getStoreName() + ".close", e.toString())); // Just log it here
982
        } finally {
983
            this.dbConnection = null;
Line 798 Link Here
798
        return conn;
Line 818 Link Here
818
        this.conn = getConnection();
1005
        this.dbConnection = getConnection();
819
--
Lines 830-884 Link Here
830
        if(conn != null) {
1017
        if (dbConnection != null) {
831
            try {
832
                conn.commit();
833
            } catch (SQLException e) {
834
                ;
835
            }
836
837
            if( preparedSizeSql != null ) {
838
                try {
839
                    preparedSizeSql.close();
840
                } catch (SQLException e) {
841
                    ;
842
                }
843
            }
844
845
            if( preparedKeysSql != null ) { 
846
                try {
847
                    preparedKeysSql.close();
848
                } catch (SQLException e) {
849
                    ;
850
                }
851
            }
852
853
            if( preparedSaveSql != null ) { 
854
                try {
855
                    preparedSaveSql.close();
856
                } catch (SQLException e) {
857
                    ;
858
                }
859
            }
860
861
            if( preparedClearSql != null ) { 
862
                try {
863
                    preparedClearSql.close();
864
                } catch (SQLException e) {
865
                    ;
866
                }
867
            }
868
869
            if( preparedRemoveSql != null ) { 
870
                try {
871
                    preparedRemoveSql.close();
872
                } catch (SQLException e) {
873
                    ;
874
                }
875
            }
876
877
            if( preparedLoadSql != null ) { 
878
                try {
879
                    preparedLoadSql.close();
880
                } catch (SQLException e) {
881
                    ;
882
                }
883
            }
884
885
--
Line 886 Link Here
886
                conn.close();
1019
                dbConnection.commit();
887
--
Lines 890-897 Link Here
890
1023
            close(dbConnection);
891
            this.preparedSizeSql = null;
892
            this.preparedKeysSql = null;
893
            this.preparedSaveSql = null;
894
            this.preparedClearSql = null;
895
            this.preparedRemoveSql = null;
896
            this.preparedLoadSql = null;
897
            this.conn = null;
898
--

Return to bug 25889