org.eclipse.persistence.sessions.server.ServerSession Java Examples

The following examples show how to use org.eclipse.persistence.sessions.server.ServerSession. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DataGenerator.java    From TeaStore with Apache License 2.0 5 votes vote down vote up
/**
 * Drops database and recreates all tables.<br/>
 * Attention: Does not reset foreign persistence contexts.
 * Best practice is to call CacheManager.MANAGER.resetAllEMFs() after dropping and then recreating the DB.
 */
public void dropAndCreateTables() {
	CacheManager.MANAGER.clearLocalCacheOnly();
	ServerSession session = CategoryRepository.REPOSITORY.getEM().unwrap(ServerSession.class);
	SchemaManager schemaManager = new SchemaManager(session);
	schemaManager.replaceDefaultTables(true, true);
	CacheManager.MANAGER.clearLocalCacheOnly();
	CacheManager.MANAGER.resetLocalEMF();
	setGenerationFinishedFlag(false);
	CacheManager.MANAGER.clearAllCaches();
}
 
Example #2
Source File: DatabaseReadOnlyNetwork.java    From BotLibre with Eclipse Public License 1.0 5 votes vote down vote up
public DatabaseReadOnlyNetwork(EntityManager entityManager, boolean isShortTerm) {
	super(new EntityManagerImpl(entityManager.unwrap(ServerSession.class), SynchronizationType.UNSYNCHRONIZED), isShortTerm);
	ServerSession server = entityManager.unwrap(ServerSession.class);
	if (!server.getProperties().containsKey("network")) {
		server.setProperty("network", this);
	}
}
 
Example #3
Source File: EntityCacheTestClass.java    From cuba with Apache License 2.0 4 votes vote down vote up
@BeforeEach
    public void setUp() throws Exception {
        assertTrue(cont.getSpringAppContext() == AppContext.getApplicationContext());

        try (Transaction tx = cont.persistence().createTransaction()) {
            EntityManagerFactory emf = cont.entityManager().getDelegate().getEntityManagerFactory();

            assertTrue(cont.metadata().getTools().isCacheable(cont.metadata().getClassNN(User.class)));
            assertFalse(cont.metadata().getTools().isCacheable(cont.metadata().getClassNN(UserSubstitution.class)));

            ServerSession serverSession = ((EntityManagerFactoryDelegate) emf).getServerSession();
            ClassDescriptor descriptor = serverSession.getDescriptor(User.class);
            assertEquals(500, descriptor.getCachePolicy().getIdentityMapSize());

//            assertTrue(Boolean.valueOf((String) emf.getProperties().get("eclipselink.cache.shared.default")));
//            assertTrue(Boolean.valueOf((String) emf.getProperties().get("eclipselink.cache.shared.sec$User")));


            this.cache = (JpaCache) emf.getCache();

            group = cont.metadata().create(Group.class);
            group.setName("group-" + group.getId());
            cont.entityManager().persist(group);

            user = cont.metadata().create(User.class);
            user.setLogin("ECTest-" + user.getId());
            user.setPassword("111");
            user.setGroup(group);
            cont.entityManager().persist(user);

            user2 = cont.metadata().create(User.class);
            user2.setLogin("ECTest-" + user2.getId());
            user2.setPassword("111");
            user2.setGroup(group);
            cont.entityManager().persist(user2);

            role = cont.metadata().create(Role.class);
            role.setName("Test role");
            role.setDescription("Test role descr");
            cont.entityManager().persist(role);

            userRole = cont.metadata().create(UserRole.class);
            userRole.setRole(role);
            userRole.setUser(user);
            cont.entityManager().persist(userRole);

            userSetting = cont.metadata().create(UserSetting.class);
            userSetting.setUser(user);
            cont.entityManager().persist(userSetting);

            userSubstitution = cont.metadata().create(UserSubstitution.class);
            userSubstitution.setUser(user);
            userSubstitution.setSubstitutedUser(user2);
            cont.entityManager().persist(userSubstitution);

            compositeOne = cont.metadata().create(CompositeOne.class);
            compositeOne.setName("compositeOne");
            cont.entityManager().persist(compositeOne);

            compositeTwo = cont.metadata().create(CompositeTwo.class);
            compositeTwo.setName("compositeTwo");
            cont.entityManager().persist(compositeTwo);

            compositePropertyOne = cont.metadata().create(CompositePropertyOne.class);
            compositePropertyOne.setName("compositePropertyOne");
            compositePropertyOne.setCompositeOne(compositeOne);
            compositePropertyOne.setCompositeTwo(compositeTwo);
            cont.entityManager().persist(compositePropertyOne);

            compositePropertyTwo = cont.metadata().create(CompositePropertyTwo.class);
            compositePropertyTwo.setName("compositePropertyTwo");
            compositePropertyTwo.setCompositeTwo(compositeTwo);
            cont.entityManager().persist(compositePropertyTwo);

            tx.commit();
        }
        cache.clear();
    }
 
Example #4
Source File: QueryCacheTestClass.java    From cuba with Apache License 2.0 4 votes vote down vote up
@BeforeEach
public void setUp() throws Exception {
    assertTrue(cont.getSpringAppContext() == AppContext.getApplicationContext());
    queryCache = AppBeans.get(QueryCache.NAME);
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManagerFactory emf = cont.entityManager().getDelegate().getEntityManagerFactory();

        assertTrue(cont.metadata().getTools().isCacheable(cont.metadata().getClassNN(User.class)));
        assertFalse(cont.metadata().getTools().isCacheable(cont.metadata().getClassNN(UserSubstitution.class)));

        ServerSession serverSession = ((EntityManagerFactoryDelegate) emf).getServerSession();
        ClassDescriptor descriptor = serverSession.getDescriptor(User.class);
        assertEquals(500, descriptor.getCachePolicy().getIdentityMapSize());

        this.cache = (JpaCache) emf.getCache();

        group = cont.metadata().create(Group.class);
        group.setName("group-" + group.getId());
        cont.entityManager().persist(group);

        user = cont.metadata().create(User.class);
        user.setLogin("ECTest-" + user.getId());
        user.setPassword("111");
        user.setName("1");
        user.setGroup(group);
        cont.entityManager().persist(user);

        user2 = cont.metadata().create(User.class);
        user2.setLogin("ECTest-" + user2.getId());
        user2.setPassword("111");
        user2.setName("2");
        user2.setGroup(group);
        cont.entityManager().persist(user2);

        user3 = cont.metadata().create(User.class);
        user3.setLogin("ECTest-" + user3.getId());
        user3.setPassword("111");
        user3.setGroup(group);
        cont.entityManager().persist(user3);

        role = cont.metadata().create(Role.class);
        role.setName("TestRole");
        role.setDescription("Test role descr");
        cont.entityManager().persist(role);

        userRole = cont.metadata().create(UserRole.class);
        userRole.setRole(role);
        userRole.setUser(user);
        cont.entityManager().persist(userRole);

        userSetting = cont.metadata().create(UserSetting.class);
        userSetting.setUser(user);
        cont.entityManager().persist(userSetting);

        tx.commit();
    }

    try (Transaction tx = cont.persistence().createTransaction()) {
        cont.entityManager().remove(user3);
        tx.commit();
    }
    cache.clear();
    queryCache.invalidateAll();
}
 
Example #5
Source File: JPAMDefaultTableGenerator.java    From jeddict with Apache License 2.0 4 votes vote down vote up
/**
 * Generate a default TableCreator object from the EclipseLink project
 * object, and perform the table existence check through jdbc table
 * metadata, and filter out tables which are already in the database.
 */
public JPAMTableCreator generateFilteredDefaultTableCreator(AbstractSession session) throws DatabaseException {
    JPAMTableCreator tblCreator = generateDefaultTableCreator();

    try {
        //table exisitence check.
        java.sql.Connection conn = null;
        if (session.isServerSession()) {
            //acquire a connection from the pool
            conn = ((ServerSession) session).getDefaultConnectionPool().acquireConnection().getConnection();
        } else if (session.isDatabaseSession()) {
            conn = ((DatabaseSessionImpl) session).getAccessor().getConnection();
        }
        if (conn == null) {
            //TODO: this is not pretty, connection is not obtained for some reason.
            return tblCreator;
        }
        DatabaseMetaData dbMetaData = conn.getMetaData();
        ResultSet resultSet = dbMetaData.getTables(null, dbMetaData.getUserName(), null, new String[]{"TABLE"});
        List tablesInDatabase = new ArrayList();

        while (resultSet.next()) {
            //save all tables from the database
            tablesInDatabase.add(resultSet.getString("TABLE_NAME"));
        }

        resultSet.close();

        List existedTables = new ArrayList();
        List existedTableNames = new ArrayList();
        Iterator tblDefIter = tblCreator.getTableDefinitions().iterator();

        while (tblDefIter.hasNext()) {
            TableDefinition tblDef = (TableDefinition) tblDefIter.next();

            //check if the to-be-created table is already in the database
            if (tablesInDatabase.contains(tblDef.getFullName())) {
                existedTables.add(tblDef);
                existedTableNames.add(tblDef.getFullName());
            }
        }

        if (!existedTableNames.isEmpty()) {
            session.getSessionLog().log(SessionLog.FINEST, SessionLog.DDL, "skip_create_existing_tables", existedTableNames);

            //remove the existed tables, won't create them.
            tblCreator.getTableDefinitions().removeAll(existedTables);
        }
    } catch (SQLException sqlEx) {
        throw DatabaseException.errorRetrieveDbMetadataThroughJDBCConnection();
    }

    return tblCreator;
}
 
Example #6
Source File: DatabaseMemory.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Shutdown the database.
 */
@Override
public synchronized void shutdown() {
	try {
		if (getShortTermMemory() != null) {
			getShortTermMemory().clear();
		}
		ServerSession session = null;
		if (this.entityManager != null) {
			session = this.entityManager.unwrap(ServerSession.class);
			save();
			this.entityManager.clear();
			//this.entityManager.close();
		}
		if (getFactory() != null) {
			getFactory().close();
			if (this.isFast && session != null) {
				SessionInfo info = sessions.get(this.database);
				if (info != null) {
					synchronized (sessions) {
						info.decrement();
						if (info.count <= 0) {
							sessions.remove(this.database);
						}
					}
					synchronized (info) {
						if (info.count <= 0) {
							if (session.isLoggedIn()) {
								session.logout();
							}
							// Should be the same.
							if (info.session.isLoggedIn()) {
								info.session.logout();
							}
							info.session = null;
						}
					}
				} else {
					// Should never happen.
					if (session.isLoggedIn()) {
						session.logout();
					}
				}
			}
		}
		this.bot.removeLogListener(this.listener);
		setEntityManager(null);
		setFactory(null);
	} catch (Exception exception) {
		this.bot.log(this, exception);
	}
}
 
Example #7
Source File: DatabaseMemory.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public int cacheSize() {
	return this.entityManager.unwrap(ServerSession.class).getIdentityMapAccessorInstance().getIdentityMap(BasicVertex.class).getSize();
}
 
Example #8
Source File: DatabaseMemory.java    From BotLibre with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void freeMemory() {
	this.entityManager.clear();
	this.entityManager.unwrap(ServerSession.class).getIdentityMapAccessor().initializeAllIdentityMaps();
}