Search in sources :

Example 76 with ManagedConnection

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();
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) DatabaseMetaData(java.sql.DatabaseMetaData) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException) SQLException(java.sql.SQLException) JDODataStoreException(javax.jdo.JDODataStoreException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) HashSet(java.util.HashSet)

Example 77 with ManagedConnection

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();
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Connection(java.sql.Connection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) DatabaseMetaData(java.sql.DatabaseMetaData) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException) SQLException(java.sql.SQLException) JDODataStoreException(javax.jdo.JDODataStoreException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) HashSet(java.util.HashSet)

Example 78 with ManagedConnection

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);
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ValueGenerationBlock(org.datanucleus.store.valuegenerator.ValueGenerationBlock) PreparedStatement(java.sql.PreparedStatement) ValueGenerationException(org.datanucleus.store.valuegenerator.ValueGenerationException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) SQLController(org.datanucleus.store.rdbms.SQLController) ResultSet(java.sql.ResultSet) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter)

Example 79 with ManagedConnection

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;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ValueGenerationConnectionProvider(org.datanucleus.store.valuegenerator.ValueGenerationConnectionProvider) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) MacroString(org.datanucleus.util.MacroString) ConnectionPreference(org.datanucleus.store.valuegenerator.AbstractConnectedGenerator.ConnectionPreference) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractConnectedGenerator(org.datanucleus.store.valuegenerator.AbstractConnectedGenerator)

Example 80 with ManagedConnection

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;
}
Also used : SQLException(java.sql.SQLException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ResultSet(java.sql.ResultSet) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) PreparedStatement(java.sql.PreparedStatement) MacroString(org.datanucleus.util.MacroString) NucleusException(org.datanucleus.exceptions.NucleusException) Timestamp(java.sql.Timestamp) Date(java.util.Date)

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