Search in sources :

Example 61 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class InformixAdapter method initialiseDatastore.

/**
 * Creates the auxiliary functions/procedures in the schema
 * @param conn the connection to the datastore
 */
public void initialiseDatastore(Connection conn) {
    try {
        Statement st = conn.createStatement();
        try {
            st.execute(getSTRPOSDropFunction());
        } catch (SQLException e) {
            NucleusLogger.DATASTORE.warn(Localiser.msg("051027", e));
        }
        try {
            st.execute(getSTRPOSFunction());
        } catch (SQLException e) {
            NucleusLogger.DATASTORE.warn(Localiser.msg("051027", e));
        }
        st.close();
    } catch (SQLException e) {
        NucleusLogger.DATASTORE_SCHEMA.warn("Exception when trying to initialise datastore", e);
        throw new NucleusDataStoreException(e.getMessage(), e);
    }
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) SQLException(java.sql.SQLException) Statement(java.sql.Statement)

Example 62 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class NuoDBAdapter method getSchemaName.

public String getSchemaName(Connection conn) throws SQLException {
    Statement stmt = conn.createStatement();
    try {
        String stmtText = "SELECT CURRENT_SCHEMA FROM DUAL";
        ResultSet rs = stmt.executeQuery(stmtText);
        try {
            if (!rs.next()) {
                throw new NucleusDataStoreException("No result returned from " + stmtText).setFatal();
            }
            return rs.getString(1);
        } finally {
            rs.close();
        }
    } finally {
        stmt.close();
    }
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet)

Example 63 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class OracleAdapter method getSchemaName.

public String getSchemaName(Connection conn) throws SQLException {
    Statement stmt = conn.createStatement();
    try {
        String stmtText = "SELECT SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') FROM DUAL";
        ResultSet rs = stmt.executeQuery(stmtText);
        try {
            if (!rs.next()) {
                throw new NucleusDataStoreException("No result returned from " + stmtText).setFatal();
            }
            return rs.getString(1);
        } finally {
            rs.close();
        }
    } finally {
        stmt.close();
    }
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SelectStatement(org.datanucleus.store.rdbms.sql.SelectStatement) ResultSet(java.sql.ResultSet)

Example 64 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class AbstractSchemaTransaction method execute.

/**
 * Executes the schema transaction.
 * A database connection is acquired and the {@link #execute(ClassLoaderResolver)}method is invoked.
 * If the selected isolation level is not Connection.TRANSACTION_NONE, then commit() or rollback() is
 * called on the connection according to whether the invocation succeeded or not. If the invocation
 * failed the sequence is repeated, up to a maximum of <var>maxRetries </var> times, configurable by
 * the persistence property "datanucleus.rdbms.classAdditionMaxRetries".
 * @param clr the ClassLoaderResolver
 * @exception NucleusDataStoreException If a SQL exception occurred even after "maxRetries" attempts.
 */
public final void execute(ClassLoaderResolver clr) {
    int attempts = 0;
    for (; ; ) {
        try {
            try {
                boolean succeeded = false;
                try {
                    run(clr);
                    succeeded = true;
                } finally {
                    if (conn != null) {
                        if (isolationLevel != TransactionIsolation.NONE) {
                            if (!conn.getAutoCommit()) {
                                if (succeeded) {
                                    if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
                                        NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("050053", StringUtils.toJVMIDString(conn)));
                                    }
                                    conn.commit();
                                } else {
                                    if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
                                        NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("050054", StringUtils.toJVMIDString(conn)));
                                    }
                                    conn.rollback();
                                }
                            }
                        }
                    }
                }
            } finally {
                if (conn != null) {
                    if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
                        NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("050055", StringUtils.toJVMIDString(conn)));
                    }
                    mconn.release();
                    conn = null;
                }
            }
            break;
        } catch (SQLException e) {
            if (++attempts >= maxRetries) {
                throw new NucleusDataStoreException(Localiser.msg("050056", this), e);
            }
        }
    }
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) SQLException(java.sql.SQLException)

Example 65 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class DatastoreAdapterFactory method getNewDatastoreAdapter.

/**
 * Accessor for the adapter for a specified datastore product.
 * @param clr ClassLoader resolver for resolving the adapter class
 * @param metadata Database MetaData for the RDBMS
 * @param adapterClassName Name of the class of the database adapter (null implies use autodetect)
 * @param pluginMgr the Plug-in manager
 * @return Instance of the database adapter
 */
protected DatastoreAdapter getNewDatastoreAdapter(ClassLoaderResolver clr, DatabaseMetaData metadata, String adapterClassName, PluginManager pluginMgr) {
    if (metadata == null) {
        return null;
    }
    String productName = null;
    if (adapterClassName == null) {
        // No adapter specified, so use "autodetection" based on the metadata to find the most suitable
        try {
            productName = metadata.getDatabaseProductName();
            if (productName == null) {
                NucleusLogger.DATASTORE.error(Localiser.msg("051024"));
                return null;
            }
        } catch (SQLException sqe) {
            NucleusLogger.DATASTORE.error(Localiser.msg("051025", sqe));
            return null;
        }
    }
    // Instantiate the adapter class
    final Object adapter_obj;
    try {
        Class adapterClass = getAdapterClass(pluginMgr, adapterClassName, productName, clr);
        if (adapterClass == null) {
            return null;
        }
        final Object[] ctr_args = new Object[] { metadata };
        final Class[] ctr_args_classes = new Class[] { DatabaseMetaData.class };
        // Create an instance of the datastore adapter
        final Constructor ctr = adapterClass.getConstructor(ctr_args_classes);
        try {
            adapter_obj = ctr.newInstance(ctr_args);
        } catch (InvocationTargetException ite) {
            if (ite.getTargetException() != null && ite.getTargetException() instanceof NucleusDataStoreException) {
                throw (NucleusDataStoreException) ite.getTargetException();
            }
            return null;
        } catch (Exception e) {
            NucleusLogger.DATASTORE.error(Localiser.msg("051026", adapterClassName, e));
            return null;
        }
    } catch (ClassNotResolvedException ex) {
        NucleusLogger.DATASTORE.error(Localiser.msg("051026", adapterClassName, ex));
        return null;
    } catch (NoSuchMethodException nsme) {
        NucleusLogger.DATASTORE.error(Localiser.msg("051026", adapterClassName, nsme));
        return null;
    }
    return (DatastoreAdapter) adapter_obj;
}
Also used : SQLException(java.sql.SQLException) Constructor(java.lang.reflect.Constructor) DatabaseMetaData(java.sql.DatabaseMetaData) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter)

Aggregations

NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)200 SQLException (java.sql.SQLException)98 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)74 ExecutionContext (org.datanucleus.ExecutionContext)73 PreparedStatement (java.sql.PreparedStatement)63 SQLController (org.datanucleus.store.rdbms.SQLController)60 ResultSet (java.sql.ResultSet)45 Iterator (java.util.Iterator)34 CollectionAddOperation (org.datanucleus.flush.CollectionAddOperation)34 CollectionRemoveOperation (org.datanucleus.flush.CollectionRemoveOperation)26 ObjectProvider (org.datanucleus.state.ObjectProvider)22 ArrayList (java.util.ArrayList)21 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)21 Collection (java.util.Collection)20 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)20 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)19 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)18 SCOCollectionIterator (org.datanucleus.store.types.SCOCollectionIterator)15 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)14 List (java.util.List)13