Example usage for org.springframework.jdbc.datasource LazyConnectionDataSourceProxy LazyConnectionDataSourceProxy

List of usage examples for org.springframework.jdbc.datasource LazyConnectionDataSourceProxy LazyConnectionDataSourceProxy

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource LazyConnectionDataSourceProxy LazyConnectionDataSourceProxy.

Prototype

public LazyConnectionDataSourceProxy(DataSource targetDataSource) 

Source Link

Document

Create a new LazyConnectionDataSourceProxy.

Usage

From source file:org.jamwiki.db.DatabaseConnection.java

/**
 * Static method that will configure a DataSource based on the Environment setup.
 *///w  ww  .j a v  a2s . com
private synchronized static void configDataSource() throws SQLException {
    if (dataSource != null) {
        closeConnectionPool(); // DataSource has already been created so remove it
    }
    String url = Environment.getValue(Environment.PROP_DB_URL);
    DataSource targetDataSource = null;
    if (url.startsWith("jdbc:")) {
        try {
            // Use an internal "LocalDataSource" configured from the Environment
            targetDataSource = new LocalDataSource();
        } catch (ClassNotFoundException e) {
            logger.error("Failure while configuring local data source", e);
            throw new SQLException("Failure while configuring local data source: " + e.toString());
        }
    } else {
        try {
            // Use a container DataSource obtained via JNDI lookup
            // TODO: Should try prefix java:comp/env/ if not already part of the JNDI name?
            Context ctx = new InitialContext();
            targetDataSource = (DataSource) ctx.lookup(url);
        } catch (NamingException e) {
            logger.error("Failure while configuring JNDI data source with URL: " + url, e);
            throw new SQLException(
                    "Unable to configure JNDI data source with URL " + url + ": " + e.toString());
        }
    }
    dataSource = new LazyConnectionDataSourceProxy(targetDataSource);
    transactionManager = new DataSourceTransactionManager(targetDataSource);
}

From source file:org.opennms.core.db.DataSourceFactory.java

/**
 * <p>init</p>/* w  w w.  j av  a  2 s.  co m*/
 *
 * @param dsName a {@link java.lang.String} object.
 * @throws java.io.IOException if any.
 * @throws org.exolab.castor.xml.MarshalException if any.
 * @throws org.exolab.castor.xml.ValidationException if any.
 * @throws java.lang.ClassNotFoundException if any.
 * @throws java.beans.PropertyVetoException if any.
 * @throws java.sql.SQLException if any.
 */
public static synchronized void init(final String dsName) throws IOException, MarshalException,
        ValidationException, ClassNotFoundException, PropertyVetoException, SQLException {
    if (isLoaded(dsName)) {
        // init already called - return
        // to reload, reload() will need to be called
        return;
    }

    String factoryClass = null;
    final File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.OPENNMS_DATASOURCE_CONFIG_FILE_NAME);
    DataSourceConfiguration dsc = null;
    ConnectionPool connectionPool = null;
    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(cfgFile);
        dsc = CastorUtils.unmarshal(DataSourceConfiguration.class, fileInputStream);
        connectionPool = dsc.getConnectionPool();
        if (connectionPool != null) {
            factoryClass = connectionPool.getFactory();
        }
    } finally {
        IOUtils.closeQuietly(fileInputStream);
    }

    final String configPath = cfgFile.getPath();
    ClosableDataSource dataSource = null;
    final String defaultClassName = DEFAULT_FACTORY_CLASS.getName();
    try {
        final Class<?> clazz = Class.forName(factoryClass);
        final Constructor<?> constructor = clazz.getConstructor(new Class<?>[] { String.class, String.class });
        dataSource = (ClosableDataSource) constructor.newInstance(new Object[] { configPath, dsName });
    } catch (final Throwable t) {
        LOG.debug("Unable to load {}, falling back to the default dataSource ({})", factoryClass,
                defaultClassName, t);
        try {
            final Constructor<?> constructor = ((Class<?>) DEFAULT_FACTORY_CLASS)
                    .getConstructor(new Class<?>[] { String.class, String.class });
            dataSource = (ClosableDataSource) constructor.newInstance(new Object[] { configPath, dsName });
        } catch (final Throwable cause) {
            LOG.error("Unable to load {}.", DEFAULT_FACTORY_CLASS.getName(), cause);
            throw new SQLException("Unable to load " + defaultClassName + ".", cause);
        }
    }

    final ClosableDataSource runnableDs = dataSource;
    m_closers.add(new Runnable() {
        @Override
        public void run() {
            try {
                runnableDs.close();
            } catch (final Throwable cause) {
                LOG.info("Unable to close datasource {}.", dsName, cause);
            }
        }
    });

    if (connectionPool != null) {
        dataSource.setIdleTimeout(connectionPool.getIdleTimeout());
        dataSource.setLoginTimeout(connectionPool.getLoginTimeout());
        dataSource.setMinPool(connectionPool.getMinPool());
        dataSource.setMaxPool(connectionPool.getMaxPool());
        dataSource.setMaxSize(connectionPool.getMaxSize());
    }

    // Springframework provided proxies that make working with transactions much easier
    final LazyConnectionDataSourceProxy lazyProxy = new LazyConnectionDataSourceProxy(dataSource);

    setInstance(dsName, lazyProxy);
}

From source file:org.opennms.netmgt.config.DataSourceFactory.java

/**
 * <p>init</p>/*from   w w w.j a  v a  2 s .  c  o m*/
 *
 * @param dsName a {@link java.lang.String} object.
 * @throws java.io.IOException if any.
 * @throws org.exolab.castor.xml.MarshalException if any.
 * @throws org.exolab.castor.xml.ValidationException if any.
 * @throws java.lang.ClassNotFoundException if any.
 * @throws java.beans.PropertyVetoException if any.
 * @throws java.sql.SQLException if any.
 */
public static synchronized void init(final String dsName) throws IOException, MarshalException,
        ValidationException, ClassNotFoundException, PropertyVetoException, SQLException {
    if (isLoaded(dsName)) {
        // init already called - return
        // to reload, reload() will need to be called
        return;
    }

    String factoryClass = null;
    final File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.OPENNMS_DATASOURCE_CONFIG_FILE_NAME);
    DataSourceConfiguration dsc = null;
    ConnectionPool connectionPool = null;
    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(cfgFile);
        dsc = CastorUtils.unmarshal(DataSourceConfiguration.class, fileInputStream);
        connectionPool = dsc.getConnectionPool();
        if (connectionPool != null) {
            factoryClass = connectionPool.getFactory();
        }
    } finally {
        IOUtils.closeQuietly(fileInputStream);
    }

    final String configPath = cfgFile.getPath();
    ClosableDataSource dataSource = null;
    final String defaultClassName = DEFAULT_FACTORY_CLASS.getName();
    try {
        final Class<?> clazz = Class.forName(factoryClass);
        final Constructor<?> constructor = clazz.getConstructor(new Class<?>[] { String.class, String.class });
        dataSource = (ClosableDataSource) constructor.newInstance(new Object[] { configPath, dsName });
    } catch (final Throwable t) {
        LogUtils.debugf(DataSourceFactory.class, t,
                "Unable to load %s, falling back to the default dataSource (%s)", factoryClass,
                defaultClassName);
        try {
            final Constructor<?> constructor = ((Class<?>) DEFAULT_FACTORY_CLASS)
                    .getConstructor(new Class<?>[] { String.class, String.class });
            dataSource = (ClosableDataSource) constructor.newInstance(new Object[] { configPath, dsName });
        } catch (final Throwable cause) {
            LogUtils.errorf(DataSourceFactory.class, cause, "Unable to load %s.",
                    DEFAULT_FACTORY_CLASS.getName());
            throw new SQLException("Unable to load " + defaultClassName + ".", cause);
        }
    }

    final ClosableDataSource runnableDs = dataSource;
    m_closers.add(new Runnable() {
        public void run() {
            try {
                runnableDs.close();
            } catch (final Throwable cause) {
                LogUtils.infof(DataSourceFactory.class, cause, "Unable to close datasource %s.", dsName);
            }
        }
    });

    if (connectionPool != null) {
        dataSource.setIdleTimeout(connectionPool.getIdleTimeout());
        dataSource.setLoginTimeout(connectionPool.getLoginTimeout());
        dataSource.setMinPool(connectionPool.getMinPool());
        dataSource.setMaxPool(connectionPool.getMaxPool());
        dataSource.setMaxSize(connectionPool.getMaxSize());
    }

    // Springframework provided proxies that make working with transactions much easier
    final LazyConnectionDataSourceProxy lazyProxy = new LazyConnectionDataSourceProxy(dataSource);

    setInstance(dsName, lazyProxy);
}