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

(-)JDBCStore.java (-7 / +72 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.sql.DataSource;
42
import javax.naming.*;
41
43
42
/**
44
/**
43
 * Implementation of the <code>Store</code> interface that stores
45
 * Implementation of the <code>Store</code> interface that stores
Lines 48-54 Link Here
48
 * @version $Revision$, $Date$
50
 * @version $Revision$, $Date$
49
 */
51
 */
50
52
51
public class JDBCStore
53
public class JDBCStore2
52
        extends StoreBase implements Store {
54
        extends StoreBase implements Store {
53
55
54
    /**
56
    /**
Lines 72-77 Link Here
72
    protected String threadName = "JDBCStore";
74
    protected String threadName = "JDBCStore";
73
75
74
    /**
76
    /**
77
     * The connection datasource
78
     */
79
    protected String datasourceName = null;
80
81
    /**
75
     * The connection username to use when trying to connect to the database.
82
     * The connection username to use when trying to connect to the database.
76
     */
83
     */
77
    protected String connectionName = null;
84
    protected String connectionName = null;
Lines 217-222 Link Here
217
    }
224
    }
218
225
219
    /**
226
    /**
227
     * Return the datasource name to use to connect to the database.
228
     *
229
     */
230
    public String getDatasourceName() {
231
        return datasourceName;
232
    }
233
234
    /**
235
     * Set the datasource name to use to connect to the database.
236
     *
237
     * @param datasourceName datasource name
238
     */
239
    public void setDatasourceName(String datasourceName) {
240
        this.datasourceName = datasourceName;
241
    }
242
243
    /**
220
     * Set the driver for this Store.
244
     * Set the driver for this Store.
221
     *
245
     *
222
     * @param driverName The new driver
246
     * @param driverName The new driver
Lines 745-750 Link Here
745
        InputStream in = null;
769
        InputStream in = null;
746
770
747
        synchronized (this) {
771
        synchronized (this) {
772
            // If sessions already exist in DB, remove and insert again.
773
            // TODO:
774
            // * Check if ID exists in database and if so use UPDATE.
775
            remove(session.getIdInternal());
776
            
748
            int numberOfTries = 2;
777
            int numberOfTries = 2;
749
            while (numberOfTries > 0) {
778
            while (numberOfTries > 0) {
750
                Connection _conn = getConnection();
779
                Connection _conn = getConnection();
Lines 752-762 Link Here
752
                    return;
781
                    return;
753
                }
782
                }
754
783
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 {
784
                try {
761
                    bos = new ByteArrayOutputStream();
785
                    bos = new ByteArrayOutputStream();
762
                    oos = new ObjectOutputStream(new BufferedOutputStream(bos));
786
                    oos = new ObjectOutputStream(new BufferedOutputStream(bos));
Lines 850-857 Link Here
850
     * @exception SQLException if a database error occurs
874
     * @exception SQLException if a database error occurs
851
     */
875
     */
852
    protected Connection open() throws SQLException {
876
    protected Connection open() throws SQLException {
877
        if (datasourceName == null) {
878
            dbConnection = openDirectConnection();
879
        } else {
880
            dbConnection = openJNDIConnection();
881
        }
882
        return dbConnection;
883
    }
853
884
885
    /**
886
     * Open (if necessary) and return a database connection for use by
887
     * this Realm.
888
     *
889
     * @exception SQLException if a database error occurs
890
     */
891
    protected Connection openJNDIConnection() throws SQLException {
892
854
        // Do nothing if there is a database connection already open
893
        // Do nothing if there is a database connection already open
894
        if (dbConnection != null && !dbConnection.isClosed())
895
            return (dbConnection);
896
897
        try {
898
            Context initialContext = new InitialContext();
899
            DataSource ds = (DataSource)initialContext.lookup(datasourceName);
900
            return ds.getConnection();
901
        } catch (NamingException ex) { 
902
           throw new SQLException("Unable to get datasource " +
903
               datasourceName + " : " + ex.getMessage());
904
        }
905
906
    }
907
908
909
    /**
910
     * Open (if necessary) and return a database connection for use by
911
     * this Realm.
912
     *
913
     * @exception SQLException if a database error occurs
914
     */
915
    protected Connection openDirectConnection() throws SQLException {
916
917
        // Do nothing if there is a database connection already open
855
        if (dbConnection != null)
918
        if (dbConnection != null)
856
            return (dbConnection);
919
            return (dbConnection);
857
920
Lines 955-961 Link Here
955
     * @param conn The connection to be released
1018
     * @param conn The connection to be released
956
     */
1019
     */
957
    protected void release(Connection conn) {
1020
    protected void release(Connection conn) {
958
        ;
1021
        if (datasourceName != null) {
1022
            close(conn);
1023
        }
959
    }
1024
    }
960
1025
961
    /**
1026
    /**

Return to bug 47061