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);
}
}
}
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);
}
}
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();
}
}
}
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;
}
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;
}
}
Aggregations