use of org.eclipse.persistence.descriptors.CacheIndex in project eclipselink by eclipse-ee4j.
the class CacheIndexMetadata method process.
/**
* INTERNAL:
* Process the index metadata
*/
public void process(MetadataDescriptor descriptor, String defaultColumnName) {
CacheIndex index = new CacheIndex();
if (m_columnNames.isEmpty() && (defaultColumnName != null)) {
index.addField(getField(defaultColumnName));
} else {
for (String column : m_columnNames) {
index.addField(getField(column));
}
}
if (this.updateable != null) {
index.setIsUpdateable(this.updateable);
}
descriptor.getClassDescriptor().getCachePolicy().addCacheIndex(index);
}
use of org.eclipse.persistence.descriptors.CacheIndex 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();
}
}
use of org.eclipse.persistence.descriptors.CacheIndex in project eclipselink by eclipse-ee4j.
the class OracleChangeNotificationListener method initialize.
/**
* Initialize the descriptor to receive database change events.
* This is called when the descriptor is initialized.
*/
@Override
public void initialize(final ClassDescriptor descriptor, AbstractSession session) {
if (descriptor.getOptimisticLockingPolicy() == null) {
boolean requiresLocking = descriptor.hasMultipleTables();
for (DatabaseMapping mapping : descriptor.getMappings()) {
if (mapping.isCollectionMapping()) {
requiresLocking = true;
}
}
if (requiresLocking) {
session.log(SessionLog.WARNING, SessionLog.METADATA, "locking_required_for_database_change_notification", descriptor.getJavaClass());
}
}
final DatabaseField rowId = descriptor.buildField(new DatabaseField(ROWID));
final List<DatabaseField> fields = new ArrayList<DatabaseField>();
fields.add(rowId);
// May already have the index if has inheritance.
CacheIndex existingIndex = descriptor.getCachePolicy().getCacheIndex(fields);
if (existingIndex == null) {
if (descriptor.isChildDescriptor()) {
existingIndex = descriptor.getInheritancePolicy().getRootParentDescriptor().getCachePolicy().getCacheIndex(fields);
}
if (existingIndex == null) {
existingIndex = new CacheIndex(fields);
existingIndex.setIsUpdateable(false);
existingIndex.setIsInsertable(false);
}
descriptor.getCachePolicy().addCacheIndex(existingIndex);
}
final CacheIndex index = existingIndex;
rowId.setInsertable(false);
rowId.setUpdatable(false);
rowId.setCreatable(false);
descriptor.getFields().add(rowId);
descriptor.getAllFields().add(rowId);
final ValueReadQuery rowIdQuery = new ValueReadQuery();
rowIdQuery.setName(ROWID);
SQLSelectStatement sqlStatement = new SQLSelectStatement();
sqlStatement.setWhereClause(descriptor.getObjectBuilder().getPrimaryKeyExpression());
sqlStatement.addField(rowId);
sqlStatement.addTable(descriptor.getTables().get(0));
rowIdQuery.setSQLStatement(sqlStatement);
sqlStatement.normalize(session, null);
descriptor.getEventManager().addListener(new DescriptorEventAdapter() {
@Override
public void postMerge(DescriptorEvent event) {
if ((event.getChangeSet() != null) && event.getChangeSet().hasChanges()) {
Object id = event.getChangeSet().getId();
CacheKey cacheKey = event.getChangeSet().getActiveCacheKey();
if (cacheKey == null) {
cacheKey = event.getSession().getParent().getIdentityMapAccessorInstance().getIdentityMapManager().getCacheKeyForObject(id, descriptor.getJavaClass(), descriptor, false);
}
cacheKey.setTransactionId(event.getSession().getProperty(ORA_TRANSACTION_ID));
if (event.getChangeSet().isNew()) {
AbstractRecord row = descriptor.getObjectBuilder().buildRowFromPrimaryKeyValues(id, event.getSession());
Object rowid = event.getSession().executeQuery(rowIdQuery, row);
CacheId indexValue = new CacheId(new Object[] { rowid });
event.getSession().getParent().getIdentityMapAccessorInstance().getIdentityMapManager().putCacheKeyByIndex(index, indexValue, cacheKey, descriptor);
}
}
}
@Override
public void postUpdate(DescriptorEvent event) {
Object txId = event.getSession().getProperty(ORA_TRANSACTION_ID);
if (txId == null) {
txId = event.getSession().executeQuery(transactionIdQuery);
event.getSession().setProperty(ORA_TRANSACTION_ID, txId);
}
}
});
}
Aggregations