Search in sources :

Example 66 with NucleusDataStoreException

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

the class DerbyAdapter 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();
        // ASCII Function
        try {
            // Try to drop the function to check existence
            st.execute("DROP FUNCTION NUCLEUS_ASCII");
        } catch (SQLException sqle) {
        }
        try {
            // Create the function
            st.execute("CREATE FUNCTION NUCLEUS_ASCII(C CHAR(1)) RETURNS INTEGER " + "EXTERNAL NAME 'org.datanucleus.store.rdbms.adapter.DerbySQLFunction.ascii' " + "CALLED ON NULL INPUT " + "LANGUAGE JAVA PARAMETER STYLE JAVA");
        } catch (SQLException sqle) {
            NucleusLogger.DATASTORE.warn(Localiser.msg("051027", sqle));
        }
        // Matches Function
        try {
            // Try to drop the function to check existence
            st.execute("DROP FUNCTION NUCLEUS_MATCHES");
        } catch (SQLException sqle) {
        }
        try {
            // Create the function
            st.execute("CREATE FUNCTION NUCLEUS_MATCHES(TEXT VARCHAR(8000), PATTERN VARCHAR(8000)) " + "RETURNS INTEGER " + "EXTERNAL NAME 'org.datanucleus.store.rdbms.adapter.DerbySQLFunction.matches' " + "CALLED ON NULL INPUT LANGUAGE JAVA PARAMETER STYLE JAVA");
        } catch (SQLException sqle) {
            NucleusLogger.DATASTORE.warn(Localiser.msg("051027", sqle));
        }
        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) SelectStatement(org.datanucleus.store.rdbms.sql.SelectStatement)

Example 67 with NucleusDataStoreException

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

the class TransactionImpl method rollback.

/**
 * Method to rollback the transaction.
 */
public void rollback() {
    if (!isActive()) {
        throw new TransactionNotActiveException();
    }
    long startTime = System.currentTimeMillis();
    try {
        // whether the transaction can be completed
        boolean canComplete = true;
        committing = true;
        try {
            // TODO Is this really needed? om.preRollback does all necessary
            flush();
        } finally {
            // even if flush fails, we ignore and go ahead cleaning up and rolling back everything ahead...
            try {
                internalPreRollback();
            } catch (NucleusUserException e) {
                // catch only NucleusUserException; they must be cascade up to user code and transaction is still alive
                if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
                    NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
                }
                canComplete = false;
                throw e;
            } finally {
                if (canComplete) {
                    try {
                        internalRollback();
                    } finally {
                        try {
                            active = false;
                            if (ec.getStatistics() != null) {
                                ec.getStatistics().transactionRolledBack(System.currentTimeMillis() - beginTime);
                            }
                        } finally {
                            listenersPerTransaction.clear();
                            // Reset rollbackOnly flag
                            rollbackOnly = false;
                            if (sync != null) {
                                sync.afterCompletion(Status.STATUS_ROLLEDBACK);
                            }
                        }
                    }
                }
            }
        }
    } catch (NucleusUserException e) {
        throw e;
    } catch (NucleusException e) {
        throw new NucleusDataStoreException(Localiser.msg("015009"), e);
    } finally {
        committing = false;
    }
    if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
        NucleusLogger.TRANSACTION.debug(Localiser.msg("015023", System.currentTimeMillis() - startTime));
    }
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) TransactionNotActiveException(org.datanucleus.exceptions.TransactionNotActiveException) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 68 with NucleusDataStoreException

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

the class TransactionImpl method commit.

/**
 * Method to commit the transaction.
 */
public void commit() {
    if (!isActive()) {
        throw new TransactionNotActiveException();
    }
    // the exception and call rollback themselves. i.e we don't need to close the DB connection or set "active" to false.
    if (rollbackOnly) {
        // Throw an exception since can only exit via rollback
        if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
            NucleusLogger.TRANSACTION.debug(Localiser.msg("015020"));
        }
        throw new NucleusDataStoreException(Localiser.msg("015020")).setFatal();
    }
    long startTime = System.currentTimeMillis();
    boolean success = false;
    // whether the transaction can be completed
    boolean canComplete = true;
    List<Throwable> errors = new ArrayList();
    try {
        // TODO Is this needed? om.preCommit will handle flush calls
        flush();
        internalPreCommit();
        internalCommit();
        success = true;
    } catch (RollbackException e) {
        // in Transaction.Synchronization and they should cascade up to user code
        if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
            NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
        }
        errors.add(e);
    } catch (HeuristicRollbackException e) {
        // in Transaction.Synchronization and they should cascade up to user code
        if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
            NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
        }
        errors.add(e);
    } catch (HeuristicMixedException e) {
        // in Transaction.Synchronization and they should cascade up to user code
        if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
            NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
        }
        errors.add(e);
    } catch (NucleusUserException e) {
        // they must be cascade up to user code and transaction is still alive
        if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
            NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
        }
        canComplete = false;
        throw e;
    } catch (NucleusException e) {
        // in Transaction.Synchronization and they should cascade up to user code
        if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
            NucleusLogger.TRANSACTION.debug(StringUtils.getStringFromStackTrace(e));
        }
        errors.add(e);
    } finally {
        if (canComplete) {
            try {
                if (!success) {
                    rollback();
                } else {
                    internalPostCommit();
                }
            } catch (Throwable e) {
                errors.add(e);
            }
        }
    }
    if (!errors.isEmpty()) {
        throw new NucleusTransactionException(Localiser.msg("015007"), errors.toArray(new Throwable[errors.size()]));
    }
    if (NucleusLogger.TRANSACTION.isDebugEnabled()) {
        NucleusLogger.TRANSACTION.debug(Localiser.msg("015022", System.currentTimeMillis() - startTime));
    }
}
Also used : NucleusTransactionException(org.datanucleus.transaction.NucleusTransactionException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ArrayList(java.util.ArrayList) HeuristicRollbackException(org.datanucleus.transaction.HeuristicRollbackException) RollbackException(org.datanucleus.transaction.RollbackException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) HeuristicRollbackException(org.datanucleus.transaction.HeuristicRollbackException) TransactionNotActiveException(org.datanucleus.exceptions.TransactionNotActiveException) HeuristicMixedException(org.datanucleus.transaction.HeuristicMixedException) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 69 with NucleusDataStoreException

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

the class BitSetStringConverter method toMemberType.

public BitSet toMemberType(String str) {
    if (str == null) {
        return null;
    }
    BitSet set = new BitSet();
    StringTokenizer tokeniser = new StringTokenizer(str.substring(1, str.length() - 1), ",");
    while (tokeniser.hasMoreTokens()) {
        String token = tokeniser.nextToken().trim();
        try {
            int position = Integer.valueOf(token).intValue();
            set.set(position);
        } catch (NumberFormatException nfe) {
            throw new NucleusDataStoreException(Localiser.msg("016002", str, BitSet.class.getName()), nfe);
        }
    }
    return set;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) StringTokenizer(java.util.StringTokenizer) BitSet(java.util.BitSet)

Example 70 with NucleusDataStoreException

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

the class Queue method addAll.

/**
 * Method to add a collection of elements.
 * @param elements The collection of elements to add.
 * @return Whether they were added successfully.
 */
public boolean addAll(java.util.Collection elements) {
    if (useCache) {
        loadFromStore();
    }
    boolean backingSuccess = true;
    if (backingStore != null) {
        if (SCOUtils.useQueuedUpdate(ownerOP)) {
            for (Object element : elements) {
                ownerOP.getExecutionContext().addOperationToQueue(new CollectionAddOperation(ownerOP, backingStore, element));
            }
        } else {
            try {
                backingSuccess = backingStore.addAll(ownerOP, elements, useCache ? delegate.size() : -1);
            } catch (NucleusDataStoreException dse) {
                NucleusLogger.PERSISTENCE.warn(Localiser.msg("023013", "addAll", ownerMmd.getName(), dse));
                backingSuccess = false;
            }
        }
    }
    // Only make it dirty after adding the field to the datastore so we give it time
    // to be inserted - otherwise jdoPreStore on this object would have been called before completing the addition
    makeDirty();
    boolean delegateSuccess = delegate.addAll(elements);
    if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
        ownerOP.getExecutionContext().processNontransactionalUpdate();
    }
    return backingStore != null ? backingSuccess : delegateSuccess;
}
Also used : CollectionAddOperation(org.datanucleus.flush.CollectionAddOperation) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException)

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