Use private objects for synchronization in JDBC storage, because BLD200501191900
authorjsichi@netbeans.org
Wed, 19 Jan 2005 01:00:18 +0000
changeset 16548e7a1f9943da
parent 1653 2d58f43d6894
child 1655 876b227d4106
Use private objects for synchronization in JDBC storage, because
other MDR code synchronizes on the storage object externally.
mdr/extras/jdbcstorage/src/org/netbeans/mdr/persistence/jdbcimpl/JdbcPrimaryIndex.java
mdr/extras/jdbcstorage/src/org/netbeans/mdr/persistence/jdbcimpl/JdbcStorage.java
     1.1 --- a/mdr/extras/jdbcstorage/src/org/netbeans/mdr/persistence/jdbcimpl/JdbcPrimaryIndex.java	Thu Jan 13 14:50:18 2005 +0000
     1.2 +++ b/mdr/extras/jdbcstorage/src/org/netbeans/mdr/persistence/jdbcimpl/JdbcPrimaryIndex.java	Wed Jan 19 01:00:18 2005 +0000
     1.3 @@ -157,30 +157,34 @@
     1.4      }
     1.5      
     1.6      // override JdbcSinglevaluedIndex
     1.7 -    public synchronized boolean put(
     1.8 +    public boolean put(
     1.9          Object key,Object value) throws StorageException
    1.10      {
    1.11 -        MOFID mKey = makeMOFID(key);
    1.12 -        if (!exists(mKey)) {
    1.13 -            addToCache(mKey,value);
    1.14 -            return false;
    1.15 -        } else {
    1.16 -            replaceInCache(mKey,value);
    1.17 -            return true;
    1.18 +        synchronized(cache) {
    1.19 +            MOFID mKey = makeMOFID(key);
    1.20 +            if (!exists(mKey)) {
    1.21 +                addToCache(mKey,value);
    1.22 +                return false;
    1.23 +            } else {
    1.24 +                replaceInCache(mKey,value);
    1.25 +                return true;
    1.26 +            }
    1.27          }
    1.28      }
    1.29      
    1.30      // override JdbcSinglevaluedIndex
    1.31 -    public synchronized void replace(Object key, Object value)
    1.32 +    public void replace(Object key, Object value)
    1.33          throws StorageException, StorageBadRequestException
    1.34      {
    1.35 -        MOFID mKey = makeMOFID(key);
    1.36 +        synchronized(cache) {
    1.37 +            MOFID mKey = makeMOFID(key);
    1.38          
    1.39 -        if (!exists(mKey)) {
    1.40 -            noSuchRecord(mKey);
    1.41 +            if (!exists(mKey)) {
    1.42 +                noSuchRecord(mKey);
    1.43 +            }
    1.44 +
    1.45 +            replaceInCache(mKey, value);
    1.46          }
    1.47 -
    1.48 -        replaceInCache(mKey, value);
    1.49      }
    1.50      
    1.51      // override JdbcSinglevaluedIndex
    1.52 @@ -203,22 +207,24 @@
    1.53      }
    1.54  
    1.55      // override JdbcSinglevaluedIndex
    1.56 -    public synchronized Object getIfExists(Object key) throws StorageException
    1.57 +    public Object getIfExists(Object key) throws StorageException
    1.58      {
    1.59 -        MOFID mKey = makeMOFID(key);
    1.60 -        Object retval;
    1.61 -        retval = cache.get(mKey);
    1.62 -        if (retval == null) {
    1.63 -            if (cache.isDeleted(mKey)) {
    1.64 -                return null;
    1.65 +        synchronized(cache) {
    1.66 +            MOFID mKey = makeMOFID(key);
    1.67 +            Object retval;
    1.68 +            retval = cache.get(mKey);
    1.69 +            if (retval == null) {
    1.70 +                if (cache.isDeleted(mKey)) {
    1.71 +                    return null;
    1.72 +                }
    1.73 +                retval = super.getIfExists(mKey);
    1.74 +                if (retval != null) {
    1.75 +                    cache.put(mKey, retval);
    1.76 +                }
    1.77              }
    1.78 -            retval = super.getIfExists(mKey);
    1.79 -            if (retval != null) {
    1.80 -                cache.put(mKey, retval);
    1.81 -            }
    1.82 +
    1.83 +            return retval;
    1.84          }
    1.85 -
    1.86 -        return retval;
    1.87      }
    1.88      
    1.89      // override JdbcSinglevaluedIndex
    1.90 @@ -245,27 +251,31 @@
    1.91      }
    1.92  
    1.93      // override JdbcIndex
    1.94 -    public synchronized void add(
    1.95 +    public void add(
    1.96          Object key, Object value) throws StorageException
    1.97      {
    1.98 -        MOFID mKey = makeMOFID(key);
    1.99 -        if (exists(mKey)) {
   1.100 -            throw new StorageBadRequestException(
   1.101 -                MessageFormat.format("Record with key {0} already exists",
   1.102 -                    new Object[] {mKey} ) );
   1.103 +        synchronized(cache) {
   1.104 +            MOFID mKey = makeMOFID(key);
   1.105 +            if (exists(mKey)) {
   1.106 +                throw new StorageBadRequestException(
   1.107 +                    MessageFormat.format("Record with key {0} already exists",
   1.108 +                        new Object[] {mKey} ) );
   1.109 +            }
   1.110 +            addToCache(mKey, value);
   1.111          }
   1.112 -        addToCache(mKey, value);
   1.113      }
   1.114      
   1.115      // override JdbcIndex
   1.116 -    public synchronized boolean remove(Object key) throws StorageException
   1.117 +    public boolean remove(Object key) throws StorageException
   1.118      {
   1.119 -        MOFID mKey = makeMOFID(key);
   1.120 -        if (!exists(mKey)) {
   1.121 -            return false;
   1.122 -        } else {
   1.123 -            cache.remove(mKey);
   1.124 -            return true;
   1.125 +        synchronized(cache) {
   1.126 +            MOFID mKey = makeMOFID(key);
   1.127 +            if (!exists(mKey)) {
   1.128 +                return false;
   1.129 +            } else {
   1.130 +                cache.remove(mKey);
   1.131 +                return true;
   1.132 +            }
   1.133          }
   1.134      }
   1.135  }
     2.1 --- a/mdr/extras/jdbcstorage/src/org/netbeans/mdr/persistence/jdbcimpl/JdbcStorage.java	Thu Jan 13 14:50:18 2005 +0000
     2.2 +++ b/mdr/extras/jdbcstorage/src/org/netbeans/mdr/persistence/jdbcimpl/JdbcStorage.java	Wed Jan 19 01:00:18 2005 +0000
     2.3 @@ -63,6 +63,8 @@
     2.4  
     2.5      private final Map entryTypeToDataTypeMap;
     2.6  
     2.7 +    private final Object connectionMutex;
     2.8 +
     2.9      private Statement jdbcStmt;
    2.10      
    2.11      private ResultSet jdbcResultSet;
    2.12 @@ -122,6 +124,10 @@
    2.13              properties, 
    2.14              JdbcStorageFactory.STORAGE_QUERY_DUPLICATES,
    2.15              false);
    2.16 +
    2.17 +        // we use a private object for synchronization on the JDBC connection
    2.18 +        // because external code synchronizes on "this" storage object
    2.19 +        connectionMutex = new Object();
    2.20          
    2.21          boolean success = false;
    2.22          
    2.23 @@ -911,82 +917,88 @@
    2.24          }
    2.25      }
    2.26  
    2.27 -    synchronized ListIterator getResultSetIterator(
    2.28 +    ListIterator getResultSetIterator(
    2.29          LazyPreparedStatement lps,Object [] args,EntryType entryType)
    2.30          throws StorageException
    2.31      {
    2.32 -        try {
    2.33 -            PreparedStatement ps = prepareStatement(lps);
    2.34 -            bindArgs(ps,args);
    2.35 -            jdbcResultSet = ps.executeQuery();
    2.36 -            // TODO:  assert exactly one column
    2.37 -            List list = new ArrayList();
    2.38 +        synchronized(connectionMutex) {
    2.39              try {
    2.40 -                while (jdbcResultSet.next()) {
    2.41 -                    Object obj = getResultObj(entryType);
    2.42 -                    list.add(obj);
    2.43 +                PreparedStatement ps = prepareStatement(lps);
    2.44 +                bindArgs(ps,args);
    2.45 +                jdbcResultSet = ps.executeQuery();
    2.46 +                // TODO:  assert exactly one column
    2.47 +                List list = new ArrayList();
    2.48 +                try {
    2.49 +                    while (jdbcResultSet.next()) {
    2.50 +                        Object obj = getResultObj(entryType);
    2.51 +                        list.add(obj);
    2.52 +                    }
    2.53 +                } finally {
    2.54 +                    closeResultSet();
    2.55                  }
    2.56 -            } finally {
    2.57 -                closeResultSet();
    2.58 +                if (entryType == EntryType.STREAMABLE) {
    2.59 +                    // Postprocess the returned byte arrays, converting them
    2.60 +                    // into real objects.  Note that we do this outside of the
    2.61 +                    // main fetch loop to avoid nasty reentrancy issues.
    2.62 +                    ListIterator listIter = list.listIterator();
    2.63 +                    while (listIter.hasNext()) {
    2.64 +                        byte [] bytes = (byte []) listIter.next();
    2.65 +                        listIter.set(readByteArray(bytes));
    2.66 +                    }
    2.67 +                }
    2.68 +                return list.listIterator();
    2.69 +            } catch (SQLException ex) {
    2.70 +                throw newJdbcException(ex);
    2.71              }
    2.72 -            if (entryType == EntryType.STREAMABLE) {
    2.73 -                // Postprocess the returned byte arrays, converting them into
    2.74 -                // real objects.  Note that we do this outside of the
    2.75 -                // main fetch loop to avoid nasty reentrancy issues.
    2.76 -                ListIterator listIter = list.listIterator();
    2.77 -                while (listIter.hasNext()) {
    2.78 -                    byte [] bytes = (byte []) listIter.next();
    2.79 -                    listIter.set(readByteArray(bytes));
    2.80 -                }
    2.81 -            }
    2.82 -            return list.listIterator();
    2.83 -        } catch (SQLException ex) {
    2.84 -            throw newJdbcException(ex);
    2.85          }
    2.86      }
    2.87  
    2.88 -    synchronized int getResultSetCount(
    2.89 +    int getResultSetCount(
    2.90          LazyPreparedStatement lps,Object [] args)
    2.91          throws StorageException
    2.92      {
    2.93 -        try {
    2.94 -            PreparedStatement ps = prepareStatement(lps);
    2.95 -            bindArgs(ps,args);
    2.96 -            jdbcResultSet = ps.executeQuery();
    2.97 +        synchronized(connectionMutex) {
    2.98              try {
    2.99 -                int n = 0;
   2.100 -                while (jdbcResultSet.next()) {
   2.101 -                    ++n;
   2.102 +                PreparedStatement ps = prepareStatement(lps);
   2.103 +                bindArgs(ps,args);
   2.104 +                jdbcResultSet = ps.executeQuery();
   2.105 +                try {
   2.106 +                    int n = 0;
   2.107 +                    while (jdbcResultSet.next()) {
   2.108 +                        ++n;
   2.109 +                    }
   2.110 +                    return n;
   2.111 +                } finally {
   2.112 +                    closeResultSet();
   2.113                  }
   2.114 -                return n;
   2.115 -            } finally {
   2.116 -                closeResultSet();
   2.117 +            } catch (SQLException ex) {
   2.118 +                throw newJdbcException(ex);
   2.119              }
   2.120 -        } catch (SQLException ex) {
   2.121 -            throw newJdbcException(ex);
   2.122          }
   2.123      }
   2.124  
   2.125 -    synchronized int getSingletonInt(
   2.126 +    int getSingletonInt(
   2.127          LazyPreparedStatement lps,Object [] args)
   2.128          throws StorageException
   2.129      {
   2.130 -        try {
   2.131 -            PreparedStatement ps = prepareStatement(lps);
   2.132 -            bindArgs(ps,args);
   2.133 -            jdbcResultSet = ps.executeQuery();
   2.134 +        synchronized(connectionMutex) {
   2.135              try {
   2.136 -                // TODO:  assert exactly one column
   2.137 -                if (!jdbcResultSet.next()) {
   2.138 -                    return -1;
   2.139 +                PreparedStatement ps = prepareStatement(lps);
   2.140 +                bindArgs(ps,args);
   2.141 +                jdbcResultSet = ps.executeQuery();
   2.142 +                try {
   2.143 +                    // TODO:  assert exactly one column
   2.144 +                    if (!jdbcResultSet.next()) {
   2.145 +                        return -1;
   2.146 +                    }
   2.147 +                    int n = jdbcResultSet.getInt(1);
   2.148 +                    return n;
   2.149 +                } finally {
   2.150 +                    closeResultSet();
   2.151                  }
   2.152 -                int n = jdbcResultSet.getInt(1);
   2.153 -                return n;
   2.154 -            } finally {
   2.155 -                closeResultSet();
   2.156 +            } catch (SQLException ex) {
   2.157 +                throw newJdbcException(ex);
   2.158              }
   2.159 -        } catch (SQLException ex) {
   2.160 -            throw newJdbcException(ex);
   2.161          }
   2.162      }
   2.163  
   2.164 @@ -1033,15 +1045,17 @@
   2.165          return data;
   2.166      }
   2.167  
   2.168 -    synchronized int executeUpdate(LazyPreparedStatement lps,Object [] args)
   2.169 +    int executeUpdate(LazyPreparedStatement lps,Object [] args)
   2.170          throws StorageException
   2.171      {
   2.172 -        try {
   2.173 -            PreparedStatement ps = prepareStatement(lps);
   2.174 -            bindArgs(ps,args);
   2.175 -            return ps.executeUpdate();
   2.176 -        } catch (SQLException ex) {
   2.177 -            throw newJdbcException(ex);
   2.178 +        synchronized(connectionMutex) {
   2.179 +            try {
   2.180 +                PreparedStatement ps = prepareStatement(lps);
   2.181 +                bindArgs(ps,args);
   2.182 +                return ps.executeUpdate();
   2.183 +            } catch (SQLException ex) {
   2.184 +                throw newJdbcException(ex);
   2.185 +            }
   2.186          }
   2.187      }
   2.188  }