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();
}
}
}
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();
}
}
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();
}
}
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);
}
}
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);
}
}
Aggregations