Search in sources :

Example 96 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.

the class RuntimeServices method printClassesInSession.

/**
 *        This method is used to output those Class Names that have identity Maps in the Session.
 * Please note that SubClasses and aggregates will be missing from this list as they do not have
 * separate identity maps.
 */
public void printClassesInSession() {
    Vector classes = getSession().getIdentityMapAccessorInstance().getIdentityMapManager().getClassesRegistered();
    int index;
    if (classes.isEmpty()) {
        ((AbstractSession) session).log(SessionLog.INFO, SessionLog.SERVER, "jmx_mbean_runtime_services_no_classes_in_session");
        return;
    }
    for (index = 0; index < classes.size(); index++) {
        getSession().getSessionLog().log(SessionLog.FINEST, (String) classes.elementAt(index));
    }
}
Also used : Vector(java.util.Vector) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 97 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.

the class RuntimeServices method getNumberOfObjectsInAllIdentityMaps.

/**
 *        This method will SUM and return the number of objects in all Identity Maps in the session.
 */
public Integer getNumberOfObjectsInAllIdentityMaps() {
    Vector classesRegistered = getSession().getIdentityMapAccessorInstance().getIdentityMapManager().getClassesRegistered();
    String registeredClassName;
    int sum = 0;
    // Check if there aren't any classes registered
    if (classesRegistered.size() == 0) {
        ((AbstractSession) session).log(SessionLog.INFO, SessionLog.SERVER, "jmx_mbean_runtime_services_no_identity_maps_in_session");
        return 0;
    }
    // get each identity map, and log the size
    for (int index = 0; index < classesRegistered.size(); index++) {
        registeredClassName = (String) classesRegistered.elementAt(index);
        try {
            sum += this.getNumberOfObjectsInIdentityMap(registeredClassName);
        } catch (ClassNotFoundException classNotFound) {
            // we are enumerating registered classes, so this shouldn't happen. Print anyway
            classNotFound.printStackTrace();
            AbstractSessionLog.getLog().log(SessionLog.SEVERE, "jmx_enabled_platform_mbean_runtime_exception", PLATFORM_NAME, classNotFound);
        }
    }
    return sum;
}
Also used : Vector(java.util.Vector) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 98 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.

the class OracleChangeNotificationListener method remove.

/**
 * INTERNAL:
 * Remove the event listener from the database.
 */
@Override
public void remove(Session session) {
    if (this.register == null) {
        return;
    }
    AbstractSession databaseSession = (AbstractSession) session;
    Accessor accessor = databaseSession.getAccessor();
    accessor.incrementCallCount(databaseSession);
    try {
        OracleConnection connection = (OracleConnection) databaseSession.getServerPlatform().unwrapConnection(accessor.getConnection());
        databaseSession.log(SessionLog.FINEST, SessionLog.CONNECTION, "dcn_unregister");
        try {
            connection.unregisterDatabaseChangeNotification(this.register);
        } catch (SQLException exception) {
            throw DatabaseException.sqlException(exception, databaseSession.getAccessor(), databaseSession, false);
        }
    } finally {
        accessor.decrementCallCount();
    }
}
Also used : SQLException(java.sql.SQLException) OracleConnection(oracle.jdbc.OracleConnection) Accessor(org.eclipse.persistence.internal.databaseaccess.Accessor) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 99 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession 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)

Example 100 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.

the class JoinedAttributeAdvancedJunitTest method internalEmployeeOuterJoinAddressPhoneProjectsTeamLeaderAddressTeamMembersPhonesOnUOW.

protected void internalEmployeeOuterJoinAddressPhoneProjectsTeamLeaderAddressTeamMembersPhonesOnUOW(boolean noBase) {
    ReadAllQuery controlQuery = new ReadAllQuery();
    controlQuery.setReferenceClass(Employee.class);
    ReadAllQuery queryWithJoins = (ReadAllQuery) controlQuery.clone();
    // Note that without the following two lines address and phones are not read not for all Employees:
    // once an Employee is built (without Address and Phones)
    // it's not going to be rebuilt (get Address and Phones) when it's
    // up again either as a teamLeader or teamMember.
    // That means that only Employees read first indirectly (either as teamLeaders or
    // teamMembers would've got Phones and Addresses).
    queryWithJoins.addJoinedAttribute(queryWithJoins.getExpressionBuilder().getAllowingNull("address"));
    queryWithJoins.addJoinedAttribute(queryWithJoins.getExpressionBuilder().anyOfAllowingNone("phoneNumbers"));
    Expression projects = queryWithJoins.getExpressionBuilder().anyOfAllowingNone("projects");
    if (!noBase) {
        queryWithJoins.addJoinedAttribute(projects);
    }
    Expression teamLeader = projects.getAllowingNull("teamLeader");
    if (!noBase) {
        queryWithJoins.addJoinedAttribute(teamLeader);
    }
    Expression teamLeaderAddress = teamLeader.getAllowingNull("address");
    queryWithJoins.addJoinedAttribute(teamLeaderAddress);
    Expression teamMembers = projects.anyOfAllowingNone("teamMembers");
    if (!noBase) {
        queryWithJoins.addJoinedAttribute(teamMembers);
    }
    Expression teamMembersPhones = teamMembers.anyOfAllowingNone("phoneNumbers");
    queryWithJoins.addJoinedAttribute(teamMembersPhones);
    UnitOfWork uow = acquireUnitOfWork();
    // loads objects into cache without using joins
    uow.commit();
    uow.executeQuery(controlQuery);
    ((UnitOfWorkImpl) uow).setShouldCascadeCloneToJoinedRelationship(true);
    Collection results = (Collection) uow.executeQuery(queryWithJoins);
    getDbSession().getIdentityMapAccessor().initializeAllIdentityMaps();
    Collection controlledResults = (Collection) JoinedAttributeTestHelper.getControlResultsFromControlQuery(controlQuery, queryWithJoins, (AbstractSession) uow);
    String errorMsg = JoinedAttributeTestHelper.compareCollections(controlledResults, results, controlQuery.getDescriptor(), (AbstractSession) getDbSession());
    if (errorMsg.length() > 0) {
        fail(errorMsg);
    }
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) Expression(org.eclipse.persistence.expressions.Expression) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) Collection(java.util.Collection) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)215 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)52 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)28 ArrayList (java.util.ArrayList)26 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)26 Vector (java.util.Vector)22 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)22 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)21 EntityManager (jakarta.persistence.EntityManager)19 List (java.util.List)18 Session (org.eclipse.persistence.sessions.Session)18 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)15 InvalidObject (org.eclipse.persistence.internal.helper.InvalidObject)14 UnitOfWorkImpl (org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)14 HashMap (java.util.HashMap)12 Map (java.util.Map)12 Collection (java.util.Collection)11 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)11 SQLException (java.sql.SQLException)10 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)9