| | |
| | | return new ReplServerDBCursor(); |
| | | } |
| | | |
| | | private void closeLockedCursor(Cursor cursor) |
| | | throws DatabaseException |
| | | { |
| | | try |
| | | { |
| | | if (cursor != null) |
| | | cursor.close(); |
| | | } |
| | | finally |
| | | { |
| | | dbCloseLock.readLock().unlock(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Read the first Change from the database. |
| | | * @return the first ChangeNumber. |
| | | */ |
| | | public ChangeNumber readFirstChange() |
| | | { |
| | | Cursor cursor; |
| | | Cursor cursor = null; |
| | | String str = null; |
| | | |
| | | try |
| | |
| | | } |
| | | try |
| | | { |
| | | try |
| | | { |
| | | DatabaseEntry key = new DatabaseEntry(); |
| | | DatabaseEntry data = new DatabaseEntry(); |
| | | OperationStatus status = cursor.getFirst(key, data, LockMode.DEFAULT); |
| | | cursor.close(); |
| | | dbCloseLock.readLock().unlock(); |
| | | if (status != OperationStatus.SUCCESS) |
| | | { |
| | | /* database is empty */ |
| | |
| | | // never happens |
| | | } |
| | | return new ChangeNumber(str); |
| | | } catch (DatabaseException e) |
| | | { |
| | | try |
| | | { |
| | | cursor.close(); |
| | | dbCloseLock.readLock().unlock(); |
| | | } |
| | | catch (DatabaseException dbe) |
| | | finally |
| | | { |
| | | // The db is dead - let's only log. |
| | | closeLockedCursor(cursor); |
| | | } |
| | | } |
| | | catch (DatabaseException e) |
| | | { |
| | | /* database is faulty */ |
| | | MessageBuilder mb = new MessageBuilder(); |
| | | mb.append(ERR_CHANGELOG_SHUTDOWN_DATABASE_ERROR.get()); |
| | |
| | | */ |
| | | public ChangeNumber readLastChange() |
| | | { |
| | | Cursor cursor; |
| | | Cursor cursor = null; |
| | | String str = null; |
| | | |
| | | try |
| | | { |
| | | dbCloseLock.readLock().lock(); |
| | | try |
| | | { |
| | | cursor = db.openCursor(null, null); |
| | | DatabaseEntry key = new DatabaseEntry(); |
| | | DatabaseEntry data = new DatabaseEntry(); |
| | | OperationStatus status = cursor.getLast(key, data, LockMode.DEFAULT); |
| | | cursor.close(); |
| | | dbCloseLock.readLock().unlock(); |
| | | if (status != OperationStatus.SUCCESS) |
| | | { |
| | | /* database is empty */ |
| | |
| | | try |
| | | { |
| | | str = new String(key.getData(), "UTF-8"); |
| | | } catch (UnsupportedEncodingException e) |
| | | } |
| | | catch (UnsupportedEncodingException e) |
| | | { |
| | | // never happens |
| | | } |
| | | return new ChangeNumber(str); |
| | | } catch (DatabaseException e) |
| | | } |
| | | finally |
| | | { |
| | | closeLockedCursor(cursor); |
| | | } |
| | | } |
| | | catch (DatabaseException e) |
| | | { |
| | | MessageBuilder mb = new MessageBuilder(); |
| | | mb.append(ERR_CHANGELOG_SHUTDOWN_DATABASE_ERROR.get()); |
| | |
| | | public class ReplServerDBCursor |
| | | { |
| | | private Cursor cursor = null; |
| | | |
| | | // The transaction that will protect the actions done with the cursor |
| | | // Will be let null for a read cursor |
| | | // Will be set non null for a write cursor |
| | | private Transaction txn = null; |
| | | DatabaseEntry key = new DatabaseEntry(); |
| | | DatabaseEntry data = new DatabaseEntry(); |
| | |
| | | OperationStatus.SUCCESS) |
| | | { |
| | | // We could not even move the cursor closed to it => failure |
| | | // Unlocking is required before throwing any exception |
| | | dbCloseLock.readLock().unlock(); |
| | | throw new Exception("ChangeNumber not available"); |
| | | } |
| | | else |
| | |
| | | if (cursor.getPrev(key, data, LockMode.DEFAULT) != |
| | | OperationStatus.SUCCESS) |
| | | { |
| | | try |
| | | { |
| | | cursor.close(); |
| | | closeLockedCursor(cursor); |
| | | dbCloseLock.readLock().lock(); |
| | | cursor = db.openCursor(txn, null); |
| | | } |
| | | catch(Exception e) |
| | | { |
| | | // Unlocking is required before throwing any exception |
| | | dbCloseLock.readLock().unlock(); |
| | | throw(e); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | catch (Exception e) |
| | | { |
| | | // Unlocking is required before throwing any exception |
| | | dbCloseLock.readLock().unlock(); |
| | | cursor.close(); |
| | | closeLockedCursor(cursor); |
| | | throw (e); |
| | | } |
| | | } |
| | |
| | | { |
| | | // We'll go on only if no close or no clear is running |
| | | dbCloseLock.readLock().lock(); |
| | | |
| | | // Create the transaction that will protect whatever done with this |
| | | // write cursor. |
| | | txn = dbenv.beginTransaction(); |
| | | |
| | | cursor = db.openCursor(txn, null); |
| | | } |
| | | catch(DatabaseException e) |
| | | { |
| | | dbCloseLock.readLock().unlock(); |
| | | if (txn != null) |
| | | { |
| | | try |
| | | { |
| | | txn.abort(); |
| | | } |
| | | catch (DatabaseException dbe) |
| | | {} |
| | | } |
| | | closeLockedCursor(cursor); |
| | | throw (e); |
| | | } |
| | | } |
| | |
| | | { |
| | | try |
| | | { |
| | | if (cursor != null) |
| | | { |
| | | cursor.close(); |
| | | closeLockedCursor(cursor); |
| | | cursor = null; |
| | | } |
| | | } |
| | | catch (DatabaseException e) |
| | | { |
| | | dbCloseLock.readLock().unlock(); |
| | | |
| | | MessageBuilder mb = new MessageBuilder(); |
| | | mb.append(ERR_CHANGELOG_SHUTDOWN_DATABASE_ERROR.get()); |
| | | mb.append(stackTraceToSingleLineString(e)); |
| | | logError(mb.toMessage()); |
| | | replicationServer.shutdown(); |
| | | } |
| | | |
| | | if (txn != null) |
| | | { |
| | | try |
| | |
| | | replicationServer.shutdown(); |
| | | } |
| | | } |
| | | dbCloseLock.readLock().unlock(); |
| | | } |
| | | |
| | | /** |
| | |
| | | return; |
| | | try |
| | | { |
| | | cursor.close(); |
| | | closeLockedCursor(cursor); |
| | | cursor = null; |
| | | } |
| | | catch (DeadlockException e1) |
| | |
| | | } |
| | | catch (DatabaseException e) |
| | | { |
| | | dbCloseLock.readLock().unlock(); |
| | | |
| | | MessageBuilder mb = new MessageBuilder(); |
| | | mb.append(ERR_CHANGELOG_SHUTDOWN_DATABASE_ERROR.get()); |
| | | mb.append(stackTraceToSingleLineString(e)); |
| | |
| | | replicationServer.shutdown(); |
| | | } |
| | | } |
| | | dbCloseLock.readLock().unlock(); |
| | | } |
| | | |
| | | /** |