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

(-)JDBCStore.java (-6 / +81 lines)
Lines 38-43 Link Here
38
import java.sql.SQLException;
38
import java.sql.SQLException;
39
import java.util.ArrayList;
39
import java.util.ArrayList;
40
import java.util.Properties;
40
import java.util.Properties;
41
import javax.naming.Context;
42
import javax.naming.InitialContext;
43
import javax.naming.NamingException;
44
import javax.sql.DataSource;
41
45
42
/**
46
/**
43
 * Implementation of the <code>Store</code> interface that stores
47
 * Implementation of the <code>Store</code> interface that stores
Lines 72-77 Link Here
72
    protected String threadName = "JDBCStore";
76
    protected String threadName = "JDBCStore";
73
77
74
    /**
78
    /**
79
     * The connection datasource
80
     */
81
    protected String datasourceName = null;
82
83
    /**
75
     * The connection username to use when trying to connect to the database.
84
     * The connection username to use when trying to connect to the database.
76
     */
85
     */
77
    protected String connectionName = null;
86
    protected String connectionName = null;
Lines 101-106 Link Here
101
     * Driver to use.
110
     * Driver to use.
102
     */
111
     */
103
    protected String driverName = null;
112
    protected String driverName = null;
113
    
114
    /**
115
     * Data source to use.
116
     */
117
    protected DataSource datasource = null;
104
118
105
    // ------------------------------------------------------------- Table & cols
119
    // ------------------------------------------------------------- Table & cols
106
120
Lines 217-222 Link Here
217
    }
231
    }
218
232
219
    /**
233
    /**
234
     * Return the datasource name to use to connect to the database.
235
     *
236
     */
237
    public String getDatasourceName() {
238
        return datasourceName;
239
    }
240
241
    /**
242
     * Set the datasource name to use to connect to the database.
243
     *
244
     * @param datasourceName datasource name
245
     */
246
    public void setDatasourceName(String datasourceName) {
247
        this.datasourceName = datasourceName;
248
    }
249
250
    /**
220
     * Set the driver for this Store.
251
     * Set the driver for this Store.
221
     *
252
     *
222
     * @param driverName The new driver
253
     * @param driverName The new driver
Lines 745-750 Link Here
745
        InputStream in = null;
776
        InputStream in = null;
746
777
747
        synchronized (this) {
778
        synchronized (this) {
779
            // If sessions already exist in DB, remove and insert again.
780
            // TODO:
781
            // * Check if ID exists in database and if so use UPDATE.
782
            remove(session.getIdInternal());
783
            
748
            int numberOfTries = 2;
784
            int numberOfTries = 2;
749
            while (numberOfTries > 0) {
785
            while (numberOfTries > 0) {
750
                Connection _conn = getConnection();
786
                Connection _conn = getConnection();
Lines 752-762 Link Here
752
                    return;
788
                    return;
753
                }
789
                }
754
790
755
                // If sessions already exist in DB, remove and insert again.
756
                // TODO:
757
                // * Check if ID exists in database and if so use UPDATE.
758
                remove(session.getIdInternal());
759
760
                try {
791
                try {
761
                    bos = new ByteArrayOutputStream();
792
                    bos = new ByteArrayOutputStream();
762
                    oos = new ObjectOutputStream(new BufferedOutputStream(bos));
793
                    oos = new ObjectOutputStream(new BufferedOutputStream(bos));
Lines 850-857 Link Here
850
     * @exception SQLException if a database error occurs
881
     * @exception SQLException if a database error occurs
851
     */
882
     */
852
    protected Connection open() throws SQLException {
883
    protected Connection open() throws SQLException {
884
        if (datasourceName == null) {
885
            dbConnection = openDirectConnection();
886
        } else {
887
            dbConnection = openJNDIConnection();
888
        }
889
        return dbConnection;
890
    }
853
891
892
    /**
893
     * Open (if necessary) and return a database connection for use by
894
     * this Realm.
895
     *
896
     * @exception SQLException if a database error occurs
897
     */
898
    protected Connection openJNDIConnection() throws SQLException {
899
854
        // Do nothing if there is a database connection already open
900
        // Do nothing if there is a database connection already open
901
        if (dbConnection != null && !dbConnection.isClosed())
902
            return (dbConnection);
903
904
        try {
905
            Context initialContext = new InitialContext();
906
            if (datasource == null) {
907
                manager.getContainer().getLogger().info("Lookup datasource " + datasourceName);
908
                datasource = (DataSource)initialContext.lookup(datasourceName);
909
            }
910
            return datasource.getConnection();
911
        } catch (NamingException ex) { 
912
           throw new SQLException("Unable to get datasource " +
913
               datasourceName + " : " + ex.getMessage());
914
        }
915
916
    }
917
918
919
    /**
920
     * Open (if necessary) and return a database connection for use by
921
     * this Realm.
922
     *
923
     * @exception SQLException if a database error occurs
924
     */
925
    protected Connection openDirectConnection() throws SQLException {
926
927
        // Do nothing if there is a database connection already open
855
        if (dbConnection != null)
928
        if (dbConnection != null)
856
            return (dbConnection);
929
            return (dbConnection);
857
930
Lines 955-961 Link Here
955
     * @param conn The connection to be released
1028
     * @param conn The connection to be released
956
     */
1029
     */
957
    protected void release(Connection conn) {
1030
    protected void release(Connection conn) {
958
        ;
1031
        if (datasourceName != null) {
1032
            close(conn);
1033
        }
959
    }
1034
    }
960
1035
961
    /**
1036
    /**

Return to bug 47061