use of org.datanucleus.store.rdbms.connectionpool.DBCP2ConnectionPoolFactory 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;
}
Aggregations