Search in sources :

Example 1 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project hive by apache.

the class ObjectStore method clearOutPmfClassLoaderCache.

private static void clearOutPmfClassLoaderCache(PersistenceManagerFactory pmf) {
    if ((pmf == null) || (!(pmf instanceof JDOPersistenceManagerFactory))) {
        return;
    }
    // NOTE : This is hacky, and this section of code is fragile depending on DN code varnames
    // so it's likely to stop working at some time in the future, especially if we upgrade DN
    // versions, so we actively need to find a better way to make sure the leak doesn't happen
    // instead of just clearing out the cache after every call.
    JDOPersistenceManagerFactory jdoPmf = (JDOPersistenceManagerFactory) pmf;
    NucleusContext nc = jdoPmf.getNucleusContext();
    try {
        Field pmCache = pmf.getClass().getDeclaredField("pmCache");
        pmCache.setAccessible(true);
        Set<JDOPersistenceManager> pmSet = (Set<JDOPersistenceManager>) pmCache.get(pmf);
        for (JDOPersistenceManager pm : pmSet) {
            org.datanucleus.ExecutionContext ec = pm.getExecutionContext();
            if (ec instanceof org.datanucleus.ExecutionContextThreadedImpl) {
                ClassLoaderResolver clr = ((org.datanucleus.ExecutionContextThreadedImpl) ec).getClassLoaderResolver();
                clearClr(clr);
            }
        }
        org.datanucleus.plugin.PluginManager pluginManager = jdoPmf.getNucleusContext().getPluginManager();
        Field registryField = pluginManager.getClass().getDeclaredField("registry");
        registryField.setAccessible(true);
        org.datanucleus.plugin.PluginRegistry registry = (org.datanucleus.plugin.PluginRegistry) registryField.get(pluginManager);
        if (registry instanceof org.datanucleus.plugin.NonManagedPluginRegistry) {
            org.datanucleus.plugin.NonManagedPluginRegistry nRegistry = (org.datanucleus.plugin.NonManagedPluginRegistry) registry;
            Field clrField = nRegistry.getClass().getDeclaredField("clr");
            clrField.setAccessible(true);
            ClassLoaderResolver clr = (ClassLoaderResolver) clrField.get(nRegistry);
            clearClr(clr);
        }
        if (nc instanceof org.datanucleus.PersistenceNucleusContextImpl) {
            org.datanucleus.PersistenceNucleusContextImpl pnc = (org.datanucleus.PersistenceNucleusContextImpl) nc;
            org.datanucleus.store.types.TypeManagerImpl tm = (org.datanucleus.store.types.TypeManagerImpl) pnc.getTypeManager();
            Field clrField = tm.getClass().getDeclaredField("clr");
            clrField.setAccessible(true);
            ClassLoaderResolver clr = (ClassLoaderResolver) clrField.get(tm);
            clearClr(clr);
            Field storeMgrField = pnc.getClass().getDeclaredField("storeMgr");
            storeMgrField.setAccessible(true);
            org.datanucleus.store.rdbms.RDBMSStoreManager storeMgr = (org.datanucleus.store.rdbms.RDBMSStoreManager) storeMgrField.get(pnc);
            Field backingStoreField = storeMgr.getClass().getDeclaredField("backingStoreByMemberName");
            backingStoreField.setAccessible(true);
            Map<String, Store> backingStoreByMemberName = (Map<String, Store>) backingStoreField.get(storeMgr);
            for (Store store : backingStoreByMemberName.values()) {
                org.datanucleus.store.rdbms.scostore.BaseContainerStore baseStore = (org.datanucleus.store.rdbms.scostore.BaseContainerStore) store;
                clrField = org.datanucleus.store.rdbms.scostore.BaseContainerStore.class.getDeclaredField("clr");
                clrField.setAccessible(true);
                clr = (ClassLoaderResolver) clrField.get(baseStore);
                clearClr(clr);
            }
        }
        Field classLoaderResolverMap = AbstractNucleusContext.class.getDeclaredField("classLoaderResolverMap");
        classLoaderResolverMap.setAccessible(true);
        Map<String, ClassLoaderResolver> loaderMap = (Map<String, ClassLoaderResolver>) classLoaderResolverMap.get(nc);
        for (ClassLoaderResolver clr : loaderMap.values()) {
            clearClr(clr);
        }
        classLoaderResolverMap.set(nc, new HashMap<String, ClassLoaderResolver>());
        LOG.debug("Removed cached classloaders from DataNucleus NucleusContext");
    } catch (Exception e) {
        LOG.warn("Failed to remove cached classloaders from DataNucleus NucleusContext", e);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) PrincipalPrivilegeSet(org.apache.hadoop.hive.metastore.api.PrincipalPrivilegeSet) NucleusContext(org.datanucleus.NucleusContext) AbstractNucleusContext(org.datanucleus.AbstractNucleusContext) Store(org.datanucleus.store.scostore.Store) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) Field(java.lang.reflect.Field) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) AlreadyExistsException(org.apache.hadoop.hive.metastore.api.AlreadyExistsException) InvalidInputException(org.apache.hadoop.hive.metastore.api.InvalidInputException) InvalidOperationException(org.apache.hadoop.hive.metastore.api.InvalidOperationException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) IOException(java.io.IOException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) JDOCanRetryException(javax.jdo.JDOCanRetryException) InvalidPartitionException(org.apache.hadoop.hive.metastore.api.InvalidPartitionException) UnknownPartitionException(org.apache.hadoop.hive.metastore.api.UnknownPartitionException) InvalidObjectException(org.apache.hadoop.hive.metastore.api.InvalidObjectException) JDOException(javax.jdo.JDOException) MissingTableException(org.datanucleus.store.rdbms.exceptions.MissingTableException) SQLException(java.sql.SQLException) UnknownDBException(org.apache.hadoop.hive.metastore.api.UnknownDBException) TException(org.apache.thrift.TException) JDODataStoreException(javax.jdo.JDODataStoreException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) UnknownTableException(org.apache.hadoop.hive.metastore.api.UnknownTableException) Map(java.util.Map) WeakValueMap(org.datanucleus.util.WeakValueMap) MRoleMap(org.apache.hadoop.hive.metastore.model.MRoleMap) HashMap(java.util.HashMap)

Example 2 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tutorials by eugenp.

the class GuideToJDO method QueryJPQL.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void QueryJPQL() {
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // JPQL :
        LOGGER.log(Level.INFO, "JPQL --------------------------------------------------------------");
        Query q = pm.newQuery("JPQL", "SELECT p FROM " + Product.class.getName() + " p WHERE p.name = 'Laptop'");
        List results = (List) q.execute();
        Iterator<Product> iter = results.iterator();
        while (iter.hasNext()) {
            Product p = iter.next();
            LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price });
        }
        LOGGER.log(Level.INFO, "--------------------------------------------------------------");
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) List(java.util.List) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory)

Example 3 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tutorials by eugenp.

the class GuideToJDO method UpdateProducts.

@SuppressWarnings("rawtypes")
public void UpdateProducts() {
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query query = pm.newQuery(Product.class, "name == \"Phone\"");
        Collection result = (Collection) query.execute();
        Product product = (Product) result.iterator().next();
        product.setName("Android Phone");
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory)

Example 4 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tutorials by eugenp.

the class GuideToJDO method DeleteProducts.

@SuppressWarnings("rawtypes")
public void DeleteProducts() {
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query query = pm.newQuery(Product.class, "name == \"Android Phone\"");
        Collection result = (Collection) query.execute();
        Product product = (Product) result.iterator().next();
        pm.deletePersistent(product);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory)

Example 5 with JDOPersistenceManagerFactory

use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tutorials by eugenp.

the class GuideToJDO method CreateProducts.

public void CreateProducts() {
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Product product = new Product("Tablet", 80.0);
        pm.makePersistent(product);
        Product product2 = new Product("Phone", 20.0);
        pm.makePersistent(product2);
        Product product3 = new Product("Laptop", 200.0);
        pm.makePersistent(product3);
        for (int i = 0; i < 100; i++) {
            String nam = "Product-" + i;
            double price = rnd.nextDouble();
            Product productx = new Product(nam, price);
            pm.makePersistent(productx);
        }
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory)

Aggregations

JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)40 PersistenceManager (javax.jdo.PersistenceManager)15 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)15 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)14 Transaction (javax.jdo.Transaction)14 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)12 NucleusContext (org.datanucleus.NucleusContext)10 Query (javax.jdo.Query)8 MetaDataManager (org.datanucleus.metadata.MetaDataManager)7 DN2NamingFactory (org.datanucleus.store.schema.naming.DN2NamingFactory)7 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)6 SQLException (java.sql.SQLException)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 IOException (java.io.IOException)4 List (java.util.List)4 JDOException (javax.jdo.JDOException)4 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)4 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)4 Field (java.lang.reflect.Field)3