Search in sources :

Example 61 with NucleusUserException

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

the class RDBMSPersistenceHandler method insertObjectInTable.

/**
 * Convenience method to handle the insert into the various tables that this object is persisted into.
 * @param table The table to process
 * @param op ObjectProvider for the object being inserted
 * @param clr ClassLoader resolver
 */
private void insertObjectInTable(DatastoreClass table, ObjectProvider op, ClassLoaderResolver clr) {
    if (table instanceof ClassView) {
        throw new NucleusUserException("Cannot perform InsertRequest on RDBMS view " + table);
    }
    DatastoreClass supertable = table.getSuperDatastoreClass();
    if (supertable != null) {
        // Process the superclass table first
        insertObjectInTable(supertable, op, clr);
    }
    // Do the actual insert of this table
    getInsertRequest(table, op.getClassMetaData(), clr).execute(op);
    // Process any secondary tables
    Collection<SecondaryDatastoreClass> secondaryTables = table.getSecondaryDatastoreClasses();
    if (secondaryTables != null) {
        for (SecondaryDatastoreClass secTable : secondaryTables) {
            // Process the secondary table
            insertObjectInTable(secTable, op, clr);
        }
    }
}
Also used : ClassView(org.datanucleus.store.rdbms.table.ClassView) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SecondaryDatastoreClass(org.datanucleus.store.rdbms.table.SecondaryDatastoreClass) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass) SecondaryDatastoreClass(org.datanucleus.store.rdbms.table.SecondaryDatastoreClass)

Example 62 with NucleusUserException

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

the class RDBMSPersistenceHandler method deleteObjectFromTable.

/**
 * Convenience method to handle the delete from the various tables that this object is persisted into.
 * @param table The table to process
 * @param sm ObjectProvider for the object being deleted
 * @param clr ClassLoader resolver
 */
private void deleteObjectFromTable(DatastoreClass table, ObjectProvider sm, ClassLoaderResolver clr) {
    if (table instanceof ClassView) {
        throw new NucleusUserException("Cannot perform DeleteRequest on RDBMS view " + table);
    }
    // Delete any secondary tables
    Collection<SecondaryDatastoreClass> secondaryTables = table.getSecondaryDatastoreClasses();
    if (secondaryTables != null) {
        for (SecondaryDatastoreClass secTable : secondaryTables) {
            // Process the secondary table
            deleteObjectFromTable(secTable, sm, clr);
        }
    }
    // Do the actual delete of this table
    getDeleteRequest(table, sm.getClassMetaData(), clr).execute(sm);
    DatastoreClass supertable = table.getSuperDatastoreClass();
    if (supertable != null) {
        // Process the superclass table last
        deleteObjectFromTable(supertable, sm, clr);
    }
}
Also used : ClassView(org.datanucleus.store.rdbms.table.ClassView) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SecondaryDatastoreClass(org.datanucleus.store.rdbms.table.SecondaryDatastoreClass) SecondaryDatastoreClass(org.datanucleus.store.rdbms.table.SecondaryDatastoreClass) DatastoreClass(org.datanucleus.store.rdbms.table.DatastoreClass)

Example 63 with NucleusUserException

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

the class ConnectionFactoryImpl method initialiseDataSource.

/**
 * Method to initialise the DataSource used by this ConnectionFactory.
 * Only invoked when the request for the first connection comes in.
 */
protected synchronized void initialiseDataSource() {
    if (getResourceName().equals(RESOURCE_NAME_TX)) {
        // Transactional
        String requiredPoolingType = storeMgr.getStringProperty(PropertyNames.PROPERTY_CONNECTION_POOLINGTYPE);
        Object connDS = storeMgr.getConnectionFactory();
        String connJNDI = storeMgr.getConnectionFactoryName();
        String connURL = storeMgr.getConnectionURL();
        dataSource = generateDataSource(storeMgr, connDS, connJNDI, getResourceName(), requiredPoolingType, connURL);
        if (dataSource == null) {
            throw new NucleusUserException(Localiser.msg("047009", "transactional")).setFatal();
        }
    } else {
        // Non-transactional
        String requiredPoolingType = storeMgr.getStringProperty(PropertyNames.PROPERTY_CONNECTION_POOLINGTYPE2);
        if (requiredPoolingType == null) {
            requiredPoolingType = storeMgr.getStringProperty(PropertyNames.PROPERTY_CONNECTION_POOLINGTYPE);
        }
        Object connDS = storeMgr.getConnectionFactory2();
        String connJNDI = storeMgr.getConnectionFactory2Name();
        String connURL = storeMgr.getConnectionURL();
        dataSource = generateDataSource(storeMgr, connDS, connJNDI, getResourceName(), requiredPoolingType, connURL);
        if (dataSource == null) {
            // Fallback to transactional settings
            connDS = storeMgr.getConnectionFactory();
            connJNDI = storeMgr.getConnectionFactoryName();
            dataSource = generateDataSource(storeMgr, connDS, connJNDI, getResourceName(), requiredPoolingType, connURL);
        }
        if (dataSource == null) {
            throw new NucleusUserException(Localiser.msg("047009", "non-transactional")).setFatal();
        }
    }
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException)

Example 64 with NucleusUserException

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

the class ConnectionFactoryImpl method generateDataSource.

/**
 * Method to generate the datasource used by this connection factory.
 * Searches initially for a provided DataSource then, if not found, for JNDI DataSource, and finally for the DataSource at a connection URL.
 * @param storeMgr Store Manager
 * @param connDS Factory data source object
 * @param connJNDI DataSource JNDI name(s)
 * @param resourceName Resource name
 * @param requiredPoolingType Type of connection pool
 * @param connURL URL for connections
 * @return The DataSource
 */
private DataSource generateDataSource(StoreManager storeMgr, Object connDS, String connJNDI, String resourceName, String requiredPoolingType, String connURL) {
    DataSource dataSource = null;
    if (connDS != null) {
        if (!(connDS instanceof DataSource) && !(connDS instanceof XADataSource)) {
            throw new UnsupportedConnectionFactoryException(connDS);
        }
        dataSource = (DataSource) connDS;
    } else if (connJNDI != null) {
        String connectionFactoryName = connJNDI.trim();
        try {
            Object obj = new InitialContext().lookup(connectionFactoryName);
            if (!(obj instanceof DataSource) && !(obj instanceof XADataSource)) {
                throw new UnsupportedConnectionFactoryException(obj);
            }
            dataSource = (DataSource) obj;
        } catch (NamingException e) {
            throw new ConnectionFactoryNotFoundException(connectionFactoryName, e);
        }
    } else if (connURL != null) {
        String poolingType = requiredPoolingType;
        if (StringUtils.isWhitespace(requiredPoolingType)) {
            // Default to dbcp2-builtin when nothing specified
            poolingType = "dbcp2-builtin";
        }
        // User has requested internal database connection pooling so check the registered plugins
        try {
            // Create the ConnectionPool to be used
            ConnectionPoolFactory connPoolFactory = null;
            // Try built-in pools first
            if (poolingType.equals("dbcp2-builtin")) {
                connPoolFactory = new DBCP2BuiltinConnectionPoolFactory();
            } else if (poolingType.equals("HikariCP")) {
                connPoolFactory = new HikariCPConnectionPoolFactory();
            } else if (poolingType.equals("BoneCP")) {
                connPoolFactory = new BoneCPConnectionPoolFactory();
            } else if (poolingType.equals("C3P0")) {
                connPoolFactory = new C3P0ConnectionPoolFactory();
            } else if (poolingType.equals("Tomcat")) {
                connPoolFactory = new TomcatConnectionPoolFactory();
            } else if (poolingType.equals("DBCP2")) {
                connPoolFactory = new DBCP2ConnectionPoolFactory();
            } else if (poolingType.equals("Proxool")) {
                connPoolFactory = new ProxoolConnectionPoolFactory();
            } else if (poolingType.equals("None")) {
                connPoolFactory = new DefaultConnectionPoolFactory();
            } else {
                // Fallback to the plugin mechanism
                connPoolFactory = (ConnectionPoolFactory) storeMgr.getNucleusContext().getPluginManager().createExecutableExtension("org.datanucleus.store.rdbms.connectionpool", "name", poolingType, "class-name", null, null);
                if (connPoolFactory == null) {
                    // User has specified a pool plugin that has not registered
                    throw new NucleusUserException(Localiser.msg("047003", poolingType)).setFatal();
                }
            }
            // Create the ConnectionPool and get the DataSource
            pool = connPoolFactory.createConnectionPool(storeMgr);
            dataSource = pool.getDataSource();
            if (NucleusLogger.CONNECTION.isDebugEnabled()) {
                NucleusLogger.CONNECTION.debug(Localiser.msg("047008", resourceName, poolingType));
            }
        } catch (ClassNotFoundException cnfe) {
            throw new NucleusUserException(Localiser.msg("047003", poolingType), cnfe).setFatal();
        } catch (Exception e) {
            if (e instanceof InvocationTargetException) {
                InvocationTargetException ite = (InvocationTargetException) e;
                throw new NucleusException(Localiser.msg("047004", poolingType, ite.getTargetException().getMessage()), ite.getTargetException()).setFatal();
            }
            throw new NucleusException(Localiser.msg("047004", poolingType, e.getMessage()), e).setFatal();
        }
    }
    return dataSource;
}
Also used : DefaultConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.DefaultConnectionPoolFactory) XADataSource(javax.sql.XADataSource) UnsupportedConnectionFactoryException(org.datanucleus.exceptions.UnsupportedConnectionFactoryException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ConnectionFactoryNotFoundException(org.datanucleus.exceptions.ConnectionFactoryNotFoundException) DBCP2ConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.DBCP2ConnectionPoolFactory) BoneCPConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.BoneCPConnectionPoolFactory) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) ConnectionFactoryNotFoundException(org.datanucleus.exceptions.ConnectionFactoryNotFoundException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) UnsupportedConnectionFactoryException(org.datanucleus.exceptions.UnsupportedConnectionFactoryException) NucleusException(org.datanucleus.exceptions.NucleusException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XAException(javax.transaction.xa.XAException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XADataSource(javax.sql.XADataSource) DataSource(javax.sql.DataSource) DBCP2BuiltinConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.DBCP2BuiltinConnectionPoolFactory) C3P0ConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.C3P0ConnectionPoolFactory) NamingException(javax.naming.NamingException) NucleusException(org.datanucleus.exceptions.NucleusException) HikariCPConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.HikariCPConnectionPoolFactory) TomcatConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.TomcatConnectionPoolFactory) ProxoolConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.ProxoolConnectionPoolFactory) DBCP2BuiltinConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.DBCP2BuiltinConnectionPoolFactory) TomcatConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.TomcatConnectionPoolFactory) HikariCPConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.HikariCPConnectionPoolFactory) ProxoolConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.ProxoolConnectionPoolFactory) DefaultConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.DefaultConnectionPoolFactory) BoneCPConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.BoneCPConnectionPoolFactory) C3P0ConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.C3P0ConnectionPoolFactory) ConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.ConnectionPoolFactory) DBCP2ConnectionPoolFactory(org.datanucleus.store.rdbms.connectionpool.DBCP2ConnectionPoolFactory)

Example 65 with NucleusUserException

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

the class ExecutionContextImpl method attachObject.

/**
 * Method to attach a persistent detached object.
 * If a different object with the same identity as this object exists in the L1 cache then an exception
 * will be thrown.
 * @param ownerOP ObjectProvider of the owner object that has this in a field that causes this attach
 * @param pc The persistable object
 * @param sco Whether the PC object is stored without an identity (embedded/serialised)
 */
public void attachObject(ObjectProvider ownerOP, Object pc, boolean sco) {
    assertClassPersistable(pc.getClass());
    // Store the owner for this persistable object being attached
    // For the current thread
    Map attachedOwnerByObject = getThreadContextInfo().attachedOwnerByObject;
    if (attachedOwnerByObject != null) {
        attachedOwnerByObject.put(pc, ownerOP);
    }
    ApiAdapter api = getApiAdapter();
    Object id = api.getIdForObject(pc);
    if (id != null && isInserting(pc)) {
        // Object is being inserted in this transaction so just return
        return;
    } else if (id == null && !sco) {
        // Transient object so needs persisting
        persistObjectInternal(pc, null, null, -1, ObjectProvider.PC);
        return;
    }
    if (api.isDetached(pc)) {
        // Detached, so migrate to attached
        if (cache != null) {
            ObjectProvider l1CachedOP = cache.get(id);
            if (l1CachedOP != null && l1CachedOP.getObject() != pc) {
                // attached object with the same id already present in the L1 cache so cannot attach in-situ
                throw new NucleusUserException(Localiser.msg("010017", StringUtils.toJVMIDString(pc)));
            }
        }
        if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
            NucleusLogger.PERSISTENCE.debug(Localiser.msg("010016", StringUtils.toJVMIDString(pc)));
        }
        ObjectProvider op = nucCtx.getObjectProviderFactory().newForDetached(this, pc, id, api.getVersionForObject(pc));
        op.attach(sco);
    } else {
        // Not detached so can't attach it. Just return
        return;
    }
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) ObjectProvider(org.datanucleus.state.ObjectProvider) ConcurrentReferenceHashMap(org.datanucleus.util.ConcurrentReferenceHashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

NucleusUserException (org.datanucleus.exceptions.NucleusUserException)258 NucleusException (org.datanucleus.exceptions.NucleusException)65 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)51 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)46 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)46 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)41 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)36 ArrayList (java.util.ArrayList)34 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)30 ObjectProvider (org.datanucleus.state.ObjectProvider)30 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)26 Expression (org.datanucleus.query.expression.Expression)24 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)23 SQLException (java.sql.SQLException)22 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)21 NullLiteral (org.datanucleus.store.rdbms.sql.expression.NullLiteral)21 SQLLiteral (org.datanucleus.store.rdbms.sql.expression.SQLLiteral)21 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)20 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)20 BigInteger (java.math.BigInteger)19