Search in sources :

Example 21 with PersistenceManager

use of javax.jdo.PersistenceManager in project tests by datanucleus.

the class JDOPersistenceTestCase method closeManagedPms.

private void closeManagedPms() {
    for (PersistenceManager pm : managedPms) {
        if (!pm.isClosed()) {
            Transaction tx = pm.currentTransaction();
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 22 with PersistenceManager

use of javax.jdo.PersistenceManager in project tests by datanucleus.

the class ReadUncommittedIsolationLevelTest method testReadUncommited.

public void testReadUncommited() {
    if (!pmf.supportedOptions().contains("javax.jdo.option.TransactionIsolationLevel.read-uncommitted")) {
        // Datastore doesn't support this isolation level
        return;
    } else if (vendorID != null && vendorID.equalsIgnoreCase("hsql")) {
        // HSQL 2.x can lock up on this
        return;
    }
    // Add some initial data
    pm = pmf.getPersistenceManager();
    pm.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "none");
    Transaction tx = pm.currentTransaction();
    Object oid;
    try {
        tx.begin();
        // create sample data
        Office o = new Office(1L, ROOM, "desc");
        o = pm.makePersistent(o);
        oid = pm.getObjectId(o);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    String finalDescription = null;
    PersistenceManager pm1 = pmf.getPersistenceManager();
    PersistenceManager pm2 = pmf.getPersistenceManager();
    Transaction tx1 = pm1.currentTransaction();
    Transaction tx2 = pm2.currentTransaction();
    try {
        pm1.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "none");
        tx1.setIsolationLevel(Constants.TX_READ_UNCOMMITTED);
        tx1.begin();
        pm2.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "none");
        tx2.setIsolationLevel(Constants.TX_READ_UNCOMMITTED);
        tx2.begin();
        Office o1 = (Office) pm1.getObjectById(oid);
        LOG.info("within tx1 after modifying:" + o1.asString());
        finalDescription = o1.getDescription() + o1.getRoomName();
        o1.setDescription(finalDescription);
        // send UPDATE to database
        pm1.flush();
        LOG.info("within tx1 after modifying:" + o1.asString());
        Office o2 = (Office) pm2.getObjectById(oid);
        LOG.info("within tx2: " + o2.asString());
        assertEquals("uncommited modification not seen", finalDescription, o2.getDescription());
    } catch (JDODataStoreException e) {
        assertFalse("Should be able to see description " + finalDescription + " but " + e.getMessage(), true);
    } finally {
        tx2.commit();
        pm2.close();
        tx1.commit();
        pm1.close();
        clean(Office.class);
        clean(Department.class);
    }
}
Also used : Office(org.jpox.samples.models.company.Office) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 23 with PersistenceManager

use of javax.jdo.PersistenceManager in project tests by datanucleus.

the class SQLFunctionPersistenceTest method testUpdateWithSQLFunction.

public void testUpdateWithSQLFunction() {
    try {
        SQLFunction function;
        Object id = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            function = new SQLFunction();
            function.setText("upper");
            function.setText1("t1");
            function.setText2("t2");
            function.setText3("t3");
            pm.makePersistent(function);
            id = JDOHelper.getObjectId(function);
            tx.commit();
            tx.begin();
            function.setText2("t2-1");
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Avoid L2 cache interference
        pmf.getDataStoreCache().evictAll(false, SQLFunction.class);
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            function = (SQLFunction) pm.getObjectById(id, true);
            assertEquals("text String retrieved is wrong", "UPPER", function.getText());
            assertEquals("text String retrieved is wrong", "T1", function.getText1());
            assertEquals("text String retrieved is wrong", "valueu", function.getText2());
            assertEquals("text String retrieved is wrong", "T3", function.getText3());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(SQLFunction.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) SQLFunction(org.jpox.samples.rdbms.sqlfunction.SQLFunction)

Example 24 with PersistenceManager

use of javax.jdo.PersistenceManager in project tests by datanucleus.

the class SchemaColumnTest method testColumnDefaultsStoringNullWhenNull.

/**
 * Test of default values for columns, storing null when a field is null at persist.
 */
public void testColumnDefaultsStoringNullWhenNull() {
    Properties props = new Properties();
    props.setProperty(RDBMSPropertyNames.PROPERTY_RDBMS_COLUMN_DEFAULT_WHEN_NULL, "false");
    PersistenceManagerFactory myPMF = getConfigurablePMF(1, props);
    try {
        PersistenceManager pm = myPMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            ClassWithDefaultCols c1 = new ClassWithDefaultCols(1);
            pm.makePersistent(c1);
            tx.commit();
        } catch (Exception ex) {
            LOG.error("Exception during test : " + ex.getMessage());
            fail("Exception thrown during test : " + ex.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        myPMF.getDataStoreCache().evictAll();
        // Retrieve and check data
        pm = myPMF.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ClassWithDefaultCols c1 = pm.getObjectById(ClassWithDefaultCols.class, 1);
            assertNull(c1.getDefaultedNameNull());
            assertNull(c1.getDefaultedName());
            assertNull(c1.getDefaultedLong());
            tx.commit();
        } catch (Exception ex) {
            LOG.error("Exception during test : " + ex.getMessage());
            fail("Exception thrown during test : " + ex.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(myPMF, ClassWithDefaultCols.class);
        myPMF.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ClassWithDefaultCols(org.datanucleus.samples.schema.ClassWithDefaultCols) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties)

Example 25 with PersistenceManager

use of javax.jdo.PersistenceManager in project tests by datanucleus.

the class SchemaHandlerTest method testForeignKeyRetrieval.

/**
 * Test of the retrieval of FKs.
 */
public void testForeignKeyRetrieval() {
    addClassesToSchema(new Class[] { SchemaClass1.class, SchemaClass2.class });
    PersistenceManager pm = pmf.getPersistenceManager();
    RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
    // Retrieve the table for SchemaClass1
    ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
    DatastoreClass table1 = databaseMgr.getDatastoreClass(SchemaClass1.class.getName(), clr);
    // Check for the FK using the schema handler
    StoreSchemaHandler handler = databaseMgr.getSchemaHandler();
    Connection con = (Connection) databaseMgr.getConnectionManager().getConnection(((JDOPersistenceManager) pm).getExecutionContext()).getConnection();
    RDBMSTableFKInfo fkInfo = (RDBMSTableFKInfo) handler.getSchemaData(con, "foreign-keys", new Object[] { table1 });
    // Expecting single FK between SchemaClass1.other and SchemaClass2
    assertEquals("Number of FKs for table " + table1 + " is wrong", 1, fkInfo.getNumberOfChildren());
    // Check the FK details
    ForeignKeyInfo fk = (ForeignKeyInfo) fkInfo.getChild(0);
    assertEquals("FK Name is wrong", "TABLE1_FK1", ((String) fk.getProperty("fk_name")).toUpperCase());
    assertEquals("PK Table Name is wrong", "SCHEMA_TABLE_2", ((String) fk.getProperty("pk_table_name")).toUpperCase());
    assertEquals("FK Table Name is wrong", "SCHEMA_TABLE_1", ((String) fk.getProperty("fk_table_name")).toUpperCase());
    assertEquals("PK Column Name is wrong", "TABLE2_ID", ((String) fk.getProperty("pk_column_name")).toUpperCase());
    assertEquals("FK Column Name is wrong", "OTHER_ID", ((String) fk.getProperty("fk_column_name")).toUpperCase());
}
Also used : ForeignKeyInfo(org.datanucleus.store.rdbms.schema.ForeignKeyInfo) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) RDBMSTableFKInfo(org.datanucleus.store.rdbms.schema.RDBMSTableFKInfo) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) Connection(java.sql.Connection) SchemaClass1(org.jpox.samples.rdbms.schema.SchemaClass1) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) StoreSchemaHandler(org.datanucleus.store.schema.StoreSchemaHandler) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)1664 Transaction (javax.jdo.Transaction)1542 Query (javax.jdo.Query)671 List (java.util.List)397 JDOUserException (javax.jdo.JDOUserException)352 Collection (java.util.Collection)309 Iterator (java.util.Iterator)239 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)185 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)170 Person (org.jpox.samples.models.company.Person)146 Employee (org.jpox.samples.models.company.Employee)128 Manager (org.jpox.samples.models.company.Manager)107 HashSet (java.util.HashSet)101 ArrayList (java.util.ArrayList)85 SQLException (java.sql.SQLException)82 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)77 JDOException (javax.jdo.JDOException)74 Extent (javax.jdo.Extent)72 JDOFatalUserException (javax.jdo.JDOFatalUserException)68 JDODataStoreException (javax.jdo.JDODataStoreException)65