use of org.datanucleus.store.connection.ManagedConnection in project tests by datanucleus.
the class SchemaTest method testArrayNonPC.
/**
* Test the schema generation for an array of primitives, stored serialised and stored in
* a join table.
*/
public void testArrayNonPC() throws Exception {
addClassesToSchema(new Class[] { IntArray.class });
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
Connection conn = null;
ManagedConnection mconn = null;
try {
tx.begin();
HashSet tableColumnNames = new HashSet();
mconn = databaseMgr.getConnectionManager().getConnection(0);
conn = (Connection) mconn.getConnection();
DatabaseMetaData dmd = conn.getMetaData();
// Main table with serialised int[]
tableColumnNames.add("INTARRAY_ID");
tableColumnNames.add("ARRAY1");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "INTARRAY", tableColumnNames);
tableColumnNames.clear();
// Join table for int[]
tableColumnNames.add("ARRAY_ID_OID");
tableColumnNames.add("INT_VALUE");
tableColumnNames.add("IDX");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "ARRAY_INTARRAY", tableColumnNames);
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Specification of table and column names for serialised array is incorrect. Exception was thrown : " + e.getMessage());
} finally {
if (conn != null) {
mconn.close();
}
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.datanucleus.store.connection.ManagedConnection in project tests by datanucleus.
the class SchemaTest method testInterfaces.
/**
* Test of the specification of table and column names for Interface types.
* Provides a test for the specification of multiple columns for a reference field
* which has multiple implementations.
*/
public void testInterfaces() throws Exception {
addClassesToSchema(new Class[] { ShapeHolder.class });
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
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 columnNames = new HashSet();
// Check table column names
columnNames.add("SHAPEHOLDER_ID");
columnNames.add("SHAPE1_SQUARE_SQUARE_ID_EID");
columnNames.add("SHAPE1_RECTANGLE_RECTANGLE_ID_EID");
columnNames.add("SHAPE1_CIRCLE_CIRCLE_ID_EID");
columnNames.add("SHAPE1_TRIANGLE_TRIANGLE_ID_EID");
columnNames.add("SHAPE2_SQUARE_SQUARE_ID_EID");
columnNames.add("SHAPE2_RECTANGLE_RECTANGLE_ID_EID");
columnNames.add("SHAPE2_CIRCLE_CIRCLE_ID_EID");
columnNames.add("SHAPE2_TRIANGLE_TRIANGLE_ID_EID");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "SHAPEHOLDER", columnNames);
columnNames.clear();
// Check Set join table
columnNames.add("SHAPEHOLDER_ID_OID");
columnNames.add("SHAPESET1_CIRCLE_CIRCLE_ID_EID");
columnNames.add("SHAPESET1_RECTANGLE_RECTANGLE_ID_EID");
columnNames.add("SHAPESET1_SQUARE_SQUARE_ID_EID");
columnNames.add("SHAPESET1_TRIANGLE_TRIANGLE_ID_EID");
// columnNames.add("IDX");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "SHAPEHOLDER_SHAPESET1", columnNames);
columnNames.clear();
// Check List join table
columnNames.add("SHAPEHOLDER_ID_OID");
columnNames.add("SHAPELIST1_CIRCLE_CIRCLE_ID_EID");
columnNames.add("SHAPELIST1_RECTANGLE_RECTANGLE_ID_EID");
columnNames.add("SHAPELIST1_SQUARE_SQUARE_ID_EID");
columnNames.add("SHAPELIST1_TRIANGLE_TRIANGLE_ID_EID");
columnNames.add("IDX");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "SHAPEHOLDER_SHAPELIST1", columnNames);
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Specification of table and column names was incorrect when using Interface type fields. Exception was thrown : " + e.getMessage());
} finally {
if (conn != null) {
mconn.close();
}
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.datanucleus.store.connection.ManagedConnection in project datanucleus-rdbms by datanucleus.
the class DatastoreUUIDHexGenerator method reserveBlock.
/**
* Reserve a block of ids.
* @param size Block size
* @return The reserved block
*/
protected synchronized ValueGenerationBlock<String> reserveBlock(long size) {
if (size < 1) {
return null;
}
List<String> oids = new ArrayList<>();
try {
ManagedConnection mconn = connectionProvider.retrieveConnection();
PreparedStatement ps = null;
ResultSet rs = null;
RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
SQLController sqlControl = rdbmsMgr.getSQLController();
try {
// Find the next ID from the database
DatastoreAdapter dba = rdbmsMgr.getDatastoreAdapter();
String stmt = dba.getSelectNewUUIDStmt();
ps = sqlControl.getStatementForQuery(mconn, stmt);
for (int i = 1; i < size; i++) {
rs = sqlControl.executeStatementQuery(null, mconn, stmt, ps);
if (rs.next()) {
oids.add(rs.getString(1));
}
}
} catch (SQLException e) {
throw new ValueGenerationException(Localiser.msg("040008", e.getMessage()));
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
sqlControl.closeStatement(mconn, ps);
}
} catch (SQLException e) {
// non-recoverable error
}
}
} finally {
connectionProvider.releaseConnection();
}
return new ValueGenerationBlock(oids);
}
use of org.datanucleus.store.connection.ManagedConnection in project datanucleus-rdbms by datanucleus.
the class RDBMSStoreManager method getNextValueForValueGenerator.
/**
* Accessor for the next value from the specified ValueGenerator.
* This implementation caters for datastore-specific generators and provides synchronisation on the connection to the datastore.
* @param generator The generator
* @param ec execution context
* @return The next value.
*/
protected Object getNextValueForValueGenerator(ValueGenerator generator, final ExecutionContext ec) {
Object oid = null;
synchronized (generator) {
// It maybe would be good to change ValueGenerator to have a next taking the connectionProvider
if (generator instanceof AbstractConnectedGenerator) {
ConnectionPreference connPref = ((AbstractConnectedGenerator) generator).getConnectionPreference();
final boolean newConnection;
if (connPref == ConnectionPreference.NONE) {
// No preference from the generator so use NEW unless overridden by the persistence property
newConnection = !getStringProperty(PropertyNames.PROPERTY_VALUEGEN_TXN_ATTRIBUTE).equalsIgnoreCase("EXISTING");
} else {
newConnection = connPref == ConnectionPreference.NEW;
}
// RDBMS-based generator so set the connection provider
ValueGenerationConnectionProvider connProvider = new ValueGenerationConnectionProvider() {
ManagedConnection mconn;
public ManagedConnection retrieveConnection() {
if (newConnection) {
mconn = connectionMgr.getConnection(TransactionUtils.getTransactionIsolationLevelForName(getStringProperty(PropertyNames.PROPERTY_VALUEGEN_TXN_ISOLATION)));
} else {
mconn = connectionMgr.getConnection(ec);
}
return mconn;
}
public void releaseConnection() {
try {
mconn.release();
mconn = null;
} catch (NucleusException e) {
String msg = Localiser.msg("050025", e);
NucleusLogger.VALUEGENERATION.error(msg);
throw new NucleusDataStoreException(msg, e);
}
}
};
((AbstractConnectedGenerator) generator).setConnectionProvider(connProvider);
}
oid = generator.next();
}
return oid;
}
use of org.datanucleus.store.connection.ManagedConnection in project datanucleus-rdbms by datanucleus.
the class RDBMSStoreManager method getDatastoreDate.
/**
* Get the date/time of the datastore.
* @return Date/time of the datastore
*/
public Date getDatastoreDate() {
Date serverDate = null;
String dateStmt = dba.getDatastoreDateStatement();
ManagedConnection mconn = null;
try {
mconn = connectionMgr.getConnection(TransactionIsolation.NONE);
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = getSQLController().getStatementForQuery(mconn, dateStmt);
rs = getSQLController().executeStatementQuery(null, mconn, dateStmt, ps);
if (rs.next()) {
// Retrieve the timestamp for the server date/time using the server TimeZone from OMF
// Assume that the dateStmt returns 1 column and is Timestamp
Timestamp time = rs.getTimestamp(1, getCalendarForDateTimezone());
serverDate = new Date(time.getTime());
} else {
return null;
}
} catch (SQLException sqle) {
String msg = Localiser.msg("050052", sqle.getMessage());
NucleusLogger.DATASTORE.warn(msg, sqle);
throw new NucleusUserException(msg, sqle).setFatal();
} finally {
if (rs != null) {
rs.close();
}
if (ps != null) {
getSQLController().closeStatement(mconn, ps);
}
}
} catch (SQLException sqle) {
String msg = Localiser.msg("050052", sqle.getMessage());
NucleusLogger.DATASTORE.warn(msg, sqle);
throw new NucleusException(msg, sqle).setFatal();
} finally {
if (mconn != null) {
mconn.release();
}
}
return serverDate;
}
Aggregations