Search in sources :

Example 1 with C3P0ConnectionPoolFactory

use of org.datanucleus.store.rdbms.connectionpool.C3P0ConnectionPoolFactory 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)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SQLException (java.sql.SQLException)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 DataSource (javax.sql.DataSource)1 XADataSource (javax.sql.XADataSource)1 XAException (javax.transaction.xa.XAException)1 ConnectionFactoryNotFoundException (org.datanucleus.exceptions.ConnectionFactoryNotFoundException)1 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 UnsupportedConnectionFactoryException (org.datanucleus.exceptions.UnsupportedConnectionFactoryException)1 BoneCPConnectionPoolFactory (org.datanucleus.store.rdbms.connectionpool.BoneCPConnectionPoolFactory)1 C3P0ConnectionPoolFactory (org.datanucleus.store.rdbms.connectionpool.C3P0ConnectionPoolFactory)1 ConnectionPoolFactory (org.datanucleus.store.rdbms.connectionpool.ConnectionPoolFactory)1 DBCP2BuiltinConnectionPoolFactory (org.datanucleus.store.rdbms.connectionpool.DBCP2BuiltinConnectionPoolFactory)1 DBCP2ConnectionPoolFactory (org.datanucleus.store.rdbms.connectionpool.DBCP2ConnectionPoolFactory)1 DefaultConnectionPoolFactory (org.datanucleus.store.rdbms.connectionpool.DefaultConnectionPoolFactory)1 HikariCPConnectionPoolFactory (org.datanucleus.store.rdbms.connectionpool.HikariCPConnectionPoolFactory)1 ProxoolConnectionPoolFactory (org.datanucleus.store.rdbms.connectionpool.ProxoolConnectionPoolFactory)1