Search in sources :

Example 1 with TableChangeDescription

use of oracle.jdbc.dcn.TableChangeDescription in project eclipselink by eclipse-ee4j.

the class OracleChangeNotificationListener method register.

/**
 * INTERNAL:
 * Register the event listener with the database.
 */
@Override
public void register(Session session) {
    final AbstractSession databaseSession = (AbstractSession) session;
    // Determine which tables should be tracked for change events.
    this.descriptorsByTable = new HashMap<DatabaseTable, ClassDescriptor>();
    for (ClassDescriptor descriptor : session.getDescriptors().values()) {
        if (!descriptor.getTables().isEmpty()) {
            if ((descriptor.getCachePolicy().getDatabaseChangeNotificationType() != null) && (descriptor.getCachePolicy().getDatabaseChangeNotificationType() != DatabaseChangeNotificationType.NONE)) {
                this.descriptorsByTable.put(descriptor.getTables().get(0), descriptor);
            }
        }
    }
    Accessor accessor = databaseSession.getAccessor();
    accessor.incrementCallCount(databaseSession);
    try {
        OracleConnection connection = (OracleConnection) databaseSession.getServerPlatform().unwrapConnection(accessor.getConnection());
        databaseSession.log(SessionLog.FINEST, SessionLog.CONNECTION, "dcn_registering");
        Properties properties = new Properties();
        properties.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
        properties.setProperty(OracleConnection.DCN_IGNORE_INSERTOP, "true");
        try {
            // Register with the database change notification, the connection is not relevant, the events occur after the connection is closed,
            // and a different connection can be used to unregister the event listener.
            this.register = connection.registerDatabaseChangeNotification(properties);
            final List<DatabaseField> fields = new ArrayList<DatabaseField>();
            fields.add(new DatabaseField(ROWID));
            this.register.addListener(new DatabaseChangeListener() {

                @Override
                public void onDatabaseChangeNotification(DatabaseChangeEvent changeEvent) {
                    databaseSession.log(SessionLog.FINEST, SessionLog.CONNECTION, "dcn_change_event", changeEvent);
                    if (changeEvent.getTableChangeDescription() != null) {
                        for (TableChangeDescription tableChange : changeEvent.getTableChangeDescription()) {
                            ClassDescriptor descriptor = OracleChangeNotificationListener.this.descriptorsByTable.get(new DatabaseTable(tableChange.getTableName()));
                            if (descriptor != null) {
                                CacheIndex index = descriptor.getCachePolicy().getCacheIndex(fields);
                                for (RowChangeDescription rowChange : tableChange.getRowChangeDescription()) {
                                    CacheId id = new CacheId(new Object[] { rowChange.getRowid().stringValue() });
                                    CacheKey key = databaseSession.getIdentityMapAccessorInstance().getIdentityMapManager().getCacheKeyByIndex(index, id, true, descriptor);
                                    if (key != null) {
                                        if ((key.getTransactionId() == null) || !key.getTransactionId().equals(changeEvent.getTransactionId(true))) {
                                            databaseSession.log(SessionLog.FINEST, SessionLog.CONNECTION, "dcn_invalidate", key.getKey(), descriptor.getJavaClass().getName());
                                            key.setInvalidationState(CacheKey.CACHE_KEY_INVALID);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            });
            // Register each table for database events, this is done by executing a select from the table.
            for (DatabaseTable table : this.descriptorsByTable.keySet()) {
                OracleStatement statement = (OracleStatement) connection.createStatement();
                statement.setDatabaseChangeRegistration(this.register);
                try {
                    statement.executeQuery("SELECT ROWID FROM " + table.getQualifiedName()).close();
                    databaseSession.log(SessionLog.FINEST, SessionLog.CONNECTION, "dcn_register_table", table.getQualifiedName());
                } catch (Exception failed) {
                    // This will fail if the table does not exist,
                    // just log the error to allow table creation to work.
                    databaseSession.logThrowable(SessionLog.WARNING, SessionLog.SQL, failed);
                } finally {
                    statement.close();
                }
            }
        } catch (SQLException exception) {
            throw DatabaseException.sqlException(exception, databaseSession.getAccessor(), databaseSession, false);
        }
    } finally {
        accessor.decrementCallCount();
    }
}
Also used : OracleStatement(oracle.jdbc.OracleStatement) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) CacheIndex(org.eclipse.persistence.descriptors.CacheIndex) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) RowChangeDescription(oracle.jdbc.dcn.RowChangeDescription) OracleConnection(oracle.jdbc.OracleConnection) Properties(java.util.Properties) Accessor(org.eclipse.persistence.internal.databaseaccess.Accessor) SQLException(java.sql.SQLException) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) DatabaseChangeEvent(oracle.jdbc.dcn.DatabaseChangeEvent) TableChangeDescription(oracle.jdbc.dcn.TableChangeDescription) DatabaseChangeListener(oracle.jdbc.dcn.DatabaseChangeListener) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) CacheKey(org.eclipse.persistence.internal.identitymaps.CacheKey) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 OracleConnection (oracle.jdbc.OracleConnection)1 OracleStatement (oracle.jdbc.OracleStatement)1 DatabaseChangeEvent (oracle.jdbc.dcn.DatabaseChangeEvent)1 DatabaseChangeListener (oracle.jdbc.dcn.DatabaseChangeListener)1 RowChangeDescription (oracle.jdbc.dcn.RowChangeDescription)1 TableChangeDescription (oracle.jdbc.dcn.TableChangeDescription)1 CacheIndex (org.eclipse.persistence.descriptors.CacheIndex)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)1 Accessor (org.eclipse.persistence.internal.databaseaccess.Accessor)1 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)1 DatabaseTable (org.eclipse.persistence.internal.helper.DatabaseTable)1 CacheId (org.eclipse.persistence.internal.identitymaps.CacheId)1 CacheKey (org.eclipse.persistence.internal.identitymaps.CacheKey)1 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)1