Search in sources :

Example 1 with JDOConnection

use of javax.jdo.datastore.JDOConnection in project hive by apache.

the class ObjectStore method prepareQuotes.

private void prepareQuotes() throws SQLException {
    if (dbType == DatabaseProduct.MYSQL) {
        assert pm.currentTransaction().isActive();
        JDOConnection jdoConn = pm.getDataStoreConnection();
        Statement statement = null;
        try {
            statement = ((Connection) jdoConn.getNativeConnection()).createStatement();
            statement.execute("SET @@session.sql_mode=ANSI_QUOTES");
        } finally {
            if (statement != null) {
                statement.close();
            }
            jdoConn.close();
        }
    }
}
Also used : Statement(java.sql.Statement) JDOConnection(javax.jdo.datastore.JDOConnection)

Example 2 with JDOConnection

use of javax.jdo.datastore.JDOConnection in project motech by motech.

the class MDSConstructorImpl method updateRequired.

private void updateRequired(String field, boolean required, String tableName, boolean isMySqlDriver) {
    JDOConnection con = persistenceManagerFactory.getPersistenceManager().getDataStoreConnection();
    Connection nativeCon = (Connection) con.getNativeConnection();
    try {
        Statement stmt = nativeCon.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        StringBuilder fieldTypeQuery = new StringBuilder("SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '");
        fieldTypeQuery.append(tableName);
        fieldTypeQuery.append("' AND COLUMN_NAME = '");
        fieldTypeQuery.append(field);
        fieldTypeQuery.append("';");
        ResultSet resultSet = stmt.executeQuery(fieldTypeQuery.toString());
        resultSet.first();
        String fieldType = resultSet.getString("DATA_TYPE");
        con.close();
        con = persistenceManagerFactory.getPersistenceManager().getDataStoreConnection();
        nativeCon = (Connection) con.getNativeConnection();
        stmt = nativeCon.createStatement();
        StringBuilder updateQuery = new StringBuilder("ALTER TABLE ");
        updateQuery.append(enquoteIfPostgres(tableName, isMySqlDriver));
        updateQuery.append(isMySqlDriver ? " MODIFY " : " ALTER COLUMN ");
        updateQuery.append(enquoteIfPostgres(field, isMySqlDriver));
        if (isMySqlDriver) {
            updateQuery.append(" ");
            updateQuery.append("varchar".equals(fieldType) ? "varchar(255)" : fieldType);
            updateQuery.append(required ? " NOT NULL" : " DEFAULT NULL");
        } else {
            updateQuery.append(required ? " SET NOT NULL" : "DROP NOT NULL");
        }
        updateQuery.append(";");
        stmt.executeUpdate(updateQuery.toString());
    } catch (SQLException e) {
        LOGGER.error(String.format("Error while updating required constraints for %s", tableName), e);
    } finally {
        con.close();
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) JDOConnection(javax.jdo.datastore.JDOConnection) ResultSet(java.sql.ResultSet) JDOConnection(javax.jdo.datastore.JDOConnection)

Example 3 with JDOConnection

use of javax.jdo.datastore.JDOConnection in project motech by motech.

the class MDSConstructorImpl method updateFieldName.

private void updateFieldName(String oldName, String newName, String tableName) {
    LOGGER.info("Renaming column in {}: {} to {}", tableName, oldName, newName);
    JDOConnection con = persistenceManagerFactory.getPersistenceManager().getDataStoreConnection();
    Connection nativeCon = (Connection) con.getNativeConnection();
    boolean isMySqlDriver = isMysql();
    try {
        Statement stmt = nativeCon.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        StringBuilder fieldTypeQuery = new StringBuilder("SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '");
        fieldTypeQuery.append(tableName);
        fieldTypeQuery.append("' AND COLUMN_NAME = '");
        fieldTypeQuery.append(oldName);
        fieldTypeQuery.append("';");
        ResultSet resultSet = stmt.executeQuery(fieldTypeQuery.toString());
        resultSet.first();
        String fieldType = resultSet.getString("DATA_TYPE");
        con.close();
        con = persistenceManagerFactory.getPersistenceManager().getDataStoreConnection();
        nativeCon = (Connection) con.getNativeConnection();
        stmt = nativeCon.createStatement();
        StringBuilder updateQuery = new StringBuilder("ALTER TABLE ");
        updateQuery.append(enquoteIfPostgres(tableName, isMySqlDriver));
        updateQuery.append(isMySqlDriver ? " CHANGE " : " RENAME COLUMN ");
        updateQuery.append(enquoteIfPostgres(oldName, isMySqlDriver));
        updateQuery.append(isMySqlDriver ? " " : " TO ");
        updateQuery.append(enquoteIfPostgres(newName, isMySqlDriver));
        if (isMySqlDriver) {
            updateQuery.append(" ");
            updateQuery.append("varchar".equals(fieldType) ? "varchar(255)" : fieldType);
        }
        updateQuery.append(";");
        stmt.executeUpdate(updateQuery.toString());
    } catch (SQLException e) {
        if ("S1000".equals(e.getSQLState())) {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info(String.format("Column %s does not exist in %s", oldName, tableName), e);
            }
        } else {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error(String.format("Unable to rename column in %s: %s to %s", tableName, oldName, newName), e);
            }
        }
    } finally {
        con.close();
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) JDOConnection(javax.jdo.datastore.JDOConnection) ResultSet(java.sql.ResultSet) JDOConnection(javax.jdo.datastore.JDOConnection)

Example 4 with JDOConnection

use of javax.jdo.datastore.JDOConnection in project tests by datanucleus.

the class ApplicationIdPersistenceTest method testAccessJdoConnection.

public void testAccessJdoConnection() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p = new Person();
            p.setPersonNum(1);
            p.setGlobalNum("1");
            p.setFirstName("Bugs");
            p.setLastName("Bunny");
            Person p2 = new Person();
            p2.setPersonNum(2);
            p2.setGlobalNum("2");
            p2.setFirstName("My");
            p2.setLastName("Friend");
            pm.makePersistent(p);
            pm.makePersistent(p2);
            JDOConnection conn = pm.getDataStoreConnection();
            assertTrue("Native connection should be instanceof com.mongodb.DB!", conn.getNativeConnection() instanceof DB);
            conn.close();
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during persist with connection access", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDOConnection(javax.jdo.datastore.JDOConnection) Person(org.jpox.samples.models.company.Person) DB(com.mongodb.DB) JDOException(javax.jdo.JDOException) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 5 with JDOConnection

use of javax.jdo.datastore.JDOConnection in project tests by datanucleus.

the class CacheTest method testCommitFailWith2ndLevelCache.

public void testCommitFailWith2ndLevelCache() throws Exception {
    if (vendorID == null) {
        // Not applicable if not RDBMS
        return;
    }
    PersistenceManager pm = null;
    Transaction tx = null;
    try {
        pm = pmf.getPersistenceManager();
        User1 a = new User1();
        a.setId("NUCCORE-1204");
        a.setName("NUCCORE-1204");
        tx = pm.currentTransaction();
        tx.begin();
        pm.makePersistent(a);
        pm.flush();
        JDOConnection con = pm.getDataStoreConnection();
        Connection o = (Connection) con.getNativeConnection();
        // emulate connection closing after flush but before commit
        o.close();
        con.close();
        try {
            tx.commit();
            fail("Commit should not succeed for closed connection: " + o);
        } catch (IllegalStateException e) {
            // commit succeeded - test failed
            throw e;
        } catch (Exception e) {
            // exception expected
            try {
                tx.rollback();
            } catch (Exception e2) {
            }
        }
        pm.close();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        User1 p = null;
        try {
            p = pm.getObjectById(User1.class, "NUCCORE-1204");
        } catch (JDOObjectNotFoundException e) {
            LOG.info("Exception caught", e);
        // ignore
        }
        if (p != null) {
            fail("Object was in 2nd level cache: " + p);
        }
        tx.commit();
        pm.close();
    } catch (Exception e) {
        LOG.error("Exception in test", e);
        fail("" + e);
    } finally {
        try {
            if (tx.isActive()) {
                tx.rollback();
            }
        } catch (Exception ignore) {
        }
        try {
            if (!pm.isClosed()) {
                pm.close();
            }
        } catch (Exception ignore) {
        }
        clean(User1.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) PersistenceManager(javax.jdo.PersistenceManager) User1(org.datanucleus.samples.metadata.user.User1) Connection(java.sql.Connection) JDOConnection(javax.jdo.datastore.JDOConnection) JDOConnection(javax.jdo.datastore.JDOConnection) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

JDOConnection (javax.jdo.datastore.JDOConnection)11 Connection (java.sql.Connection)8 SQLException (java.sql.SQLException)6 Statement (java.sql.Statement)6 ResultSet (java.sql.ResultSet)4 PersistenceManager (javax.jdo.PersistenceManager)4 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)3 Transaction (javax.jdo.Transaction)3 JDOException (javax.jdo.JDOException)2 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)2 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)2 DB (com.mongodb.DB)1 PreparedStatement (java.sql.PreparedStatement)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JDOOptimisticVerificationException (javax.jdo.JDOOptimisticVerificationException)1 JDOUnsupportedOptionException (javax.jdo.JDOUnsupportedOptionException)1 JDOUserCallbackException (javax.jdo.JDOUserCallbackException)1 JDOUserException (javax.jdo.JDOUserException)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1