Search in sources :

Example 51 with ManagedConnection

use of org.datanucleus.store.connection.ManagedConnection in project tests by datanucleus.

the class SchemaTest method testEmbeddedCollection.

/**
 * Test for JPA embedded collection elements.
 */
public void testEmbeddedCollection() throws Exception {
    addClassesToSchema(new Class[] { EmbeddedCollectionOwner.class, EmbeddedCollElement.class });
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
    Connection conn = null;
    ManagedConnection mconn = null;
    try {
        tx.begin();
        mconn = databaseMgr.getConnectionManager().getConnection(0);
        conn = (Connection) mconn.getConnection();
        DatabaseMetaData dmd = conn.getMetaData();
        // Map with embedded value taking default value column names
        Set<String> columnNames = new HashSet<String>();
        // FK to owner
        columnNames.add("JPA_COLL_EMB_OWNER_ID");
        // Element "name"
        columnNames.add("NAME");
        // Element "value"
        columnNames.add("VALUE");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_COLL_EMB", columnNames);
        // Map with embedded value overriding the value column names
        Set<String> columnNames2 = new HashSet<String>();
        // FK to owner
        columnNames2.add("JPA_COLL_EMB_OWNER_ID");
        // Element "name"
        columnNames2.add("COLL_ELEM_NAME");
        // Element "value"
        columnNames2.add("COLL_ELEM_VALUE");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_COLL_EMB_OVERRIDE", columnNames2);
    } catch (Exception e) {
        LOG.error("Exception thrown", e);
        fail("Exception thrown : " + e.getMessage());
    } finally {
        if (conn != null) {
            mconn.close();
        }
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) DatabaseMetaData(java.sql.DatabaseMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) HashSet(java.util.HashSet)

Example 52 with ManagedConnection

use of org.datanucleus.store.connection.ManagedConnection in project tests by datanucleus.

the class SchemaTest method testInheritanceStrategyTablePerClass.

/**
 * Test for JPA inheritance strategy "table-per-class" on a hierarchy of classes.
 */
public void testInheritanceStrategyTablePerClass() throws Exception {
    addClassesToSchema(new Class[] { InheritB.class, InheritB1.class, InheritB2.class });
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
    Connection conn = null;
    ManagedConnection mconn = null;
    try {
        tx.begin();
        mconn = databaseMgr.getConnectionManager().getConnection(0);
        conn = (Connection) mconn.getConnection();
        DatabaseMetaData dmd = conn.getMetaData();
        HashSet<String> columnNames = new HashSet<String>();
        columnNames.add("ID");
        columnNames.add("NAME");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_INHERIT_B", columnNames);
        columnNames = new HashSet<String>();
        columnNames.add("ID");
        columnNames.add("NAME");
        columnNames.add("NAME1");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_INHERIT_B1", columnNames);
        columnNames = new HashSet<String>();
        columnNames.add("ID");
        columnNames.add("NAME");
        columnNames.add("NAME2");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_INHERIT_B2", columnNames);
        tx.commit();
    } catch (Exception e) {
        LOG.error(e);
        fail("Specification of table and column names must have been ignored when creating the schema for " + "inheritance case where the fields were overridden. Exception was thrown : " + e.getMessage());
    } finally {
        if (conn != null) {
            mconn.close();
        }
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) DatabaseMetaData(java.sql.DatabaseMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) HashSet(java.util.HashSet)

Example 53 with ManagedConnection

use of org.datanucleus.store.connection.ManagedConnection in project tests by datanucleus.

the class SchemaTest method testInheritanceStrategySingleTable.

/**
 * Test for JPA inheritance strategy "single-table" on a hierarchy of classes.
 */
public void testInheritanceStrategySingleTable() throws Exception {
    addClassesToSchema(new Class[] { InheritC.class, InheritC1.class, InheritC2.class });
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
    Connection conn = null;
    ManagedConnection mconn = null;
    try {
        tx.begin();
        mconn = databaseMgr.getConnectionManager().getConnection(0);
        conn = (Connection) mconn.getConnection();
        DatabaseMetaData dmd = conn.getMetaData();
        HashSet<String> columnNames = new HashSet<String>();
        columnNames.add("ID");
        columnNames.add("NAME");
        columnNames.add("NAME1");
        columnNames.add("NAME2");
        columnNames.add("DTYPE");
        // Check base table column names
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_INHERIT_C", columnNames);
        tx.commit();
    } catch (Exception e) {
        LOG.error(e);
        fail("Specification of table and column names must have been ignored when creating the schema for " + "inheritance case where the fields were overridden. Exception was thrown : " + e.getMessage());
    } finally {
        if (conn != null) {
            mconn.close();
        }
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) DatabaseMetaData(java.sql.DatabaseMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) HashSet(java.util.HashSet)

Example 54 with ManagedConnection

use of org.datanucleus.store.connection.ManagedConnection in project tests by datanucleus.

the class SchemaTest method testMapOfSimpleSimpleViaXml.

/**
 * Test for JPA Map<NonPC, NonPC> using xml.
 */
public void testMapOfSimpleSimpleViaXml() throws Exception {
    addClassesToSchema(new Class[] { MapHolder1Xml.class });
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
    Connection conn = null;
    ManagedConnection mconn = null;
    try {
        tx.begin();
        mconn = databaseMgr.getConnectionManager().getConnection(0);
        conn = (Connection) mconn.getConnection();
        DatabaseMetaData dmd = conn.getMetaData();
        HashSet<String> columnNames = new HashSet<String>();
        columnNames.add("JPA_XML_MAPHOLDER1_ID");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_XML_MAPHOLDER1", columnNames);
        HashSet<String> columnNames2 = new HashSet<String>();
        columnNames2.add("MAPHOLDER1_ID");
        columnNames2.add("PROP_NAME");
        columnNames2.add("PROP_VALUE");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_XML_MAPHOLDER1_PROPS", columnNames2);
        HashSet<String> columnNames3 = new HashSet<String>();
        columnNames3.add("MAPHOLDER1XML_JPA_XML_MAPHOLDER1_ID");
        columnNames3.add("PROPERTIES2_KEY");
        columnNames3.add("PROPERTIES2_VALUE");
        RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "MAPHOLDER1XML_PROPERTIES2", columnNames3);
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception thrown", e);
        fail("Exception thrown : " + e.getMessage());
    } finally {
        if (conn != null) {
            mconn.close();
        }
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) DatabaseMetaData(java.sql.DatabaseMetaData) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) HashSet(java.util.HashSet)

Example 55 with ManagedConnection

use of org.datanucleus.store.connection.ManagedConnection in project tests by datanucleus.

the class StoredProcedureTest method testExecuteOutputParam.

public void testExecuteOutputParam() {
    if (vendorID == null) {
        return;
    }
    if (storeMgr instanceof RDBMSStoreManager) {
        DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
        if (!dba.supportsOption(DatastoreAdapter.STORED_PROCEDURES)) {
            LOG.warn("Database doesnt support stored procedures so ignoring the test");
            return;
        }
    }
    String procName = "DN_PROC_OUTPUTPARAM";
    RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
    ManagedConnection mc = rdbmsMgr.getConnectionManager().getConnection(-1);
    try {
        Connection conn = (Connection) mc.getConnection();
        Statement stmt = conn.createStatement();
        // Drop it first
        String dropStmt = "DROP PROCEDURE IF EXISTS " + procName;
        stmt.execute(dropStmt);
        // Create it
        String createStmt = "CREATE PROCEDURE " + procName + "(OUT PARAM1 INT) BEGIN " + "SELECT COUNT(*) INTO PARAM1 FROM JPA_AN_PERSON; END";
        stmt.execute(createStmt);
    } catch (SQLException sqle) {
        fail("Exception in drop-create of stored procedure : " + sqle.getMessage());
    } finally {
        mc.close();
    }
    try {
        JPAEntityManager em = (JPAEntityManager) getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
            em.persist(p);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        em = (JPAEntityManager) getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            // Get value to compare against
            Query q = em.createQuery("SELECT COUNT(p) FROM " + Person.class.getName() + " p");
            Long count = (Long) q.getSingleResult();
            // Execute stored proc and compare
            StoredProcedureQuery spq = em.createStoredProcedureQuery(procName);
            spq.registerStoredProcedureParameter("PARAM1", Integer.class, ParameterMode.OUT);
            boolean val = spq.execute();
            assertFalse("Flag for result set returned true but should have been false", val);
            Object paramVal = spq.getOutputParameterValue("PARAM1");
            assertEquals("Output parameter is incorrect", new Integer(count.intValue()), paramVal);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        // Cleanup data
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Query(javax.persistence.Query) SQLException(java.sql.SQLException) Statement(java.sql.Statement) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) JPAEntityManager(org.datanucleus.api.jpa.JPAEntityManager) SQLException(java.sql.SQLException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) Person(org.datanucleus.samples.annotations.models.company.Person)

Aggregations

ManagedConnection (org.datanucleus.store.connection.ManagedConnection)157 SQLException (java.sql.SQLException)125 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)80 Connection (java.sql.Connection)75 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)74 PreparedStatement (java.sql.PreparedStatement)70 ExecutionContext (org.datanucleus.ExecutionContext)64 SQLController (org.datanucleus.store.rdbms.SQLController)63 HashSet (java.util.HashSet)62 DatabaseMetaData (java.sql.DatabaseMetaData)58 PersistenceManager (javax.jdo.PersistenceManager)46 Transaction (javax.jdo.Transaction)46 JDOFatalUserException (javax.jdo.JDOFatalUserException)45 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)45 ResultSet (java.sql.ResultSet)37 JDOFatalInternalException (javax.jdo.JDOFatalInternalException)24 JDODataStoreException (javax.jdo.JDODataStoreException)21 JDOUserException (javax.jdo.JDOUserException)21 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)21 EntityTransaction (javax.persistence.EntityTransaction)19