Example usage for java.sql Connection TRANSACTION_NONE

List of usage examples for java.sql Connection TRANSACTION_NONE

Introduction

In this page you can find the example usage for java.sql Connection TRANSACTION_NONE.

Prototype

int TRANSACTION_NONE

To view the source code for java.sql Connection TRANSACTION_NONE.

Click Source Link

Document

A constant indicating that transactions are not supported.

Usage

From source file:org.lib4j.dbcp.DataSources.java

/**
 * Create a <code>BasicDataSource</code> given a dbcp JAXB binding.
 *
 * @param dbcp JAXB dbcp binding.//from  ww  w  .j a  va  2 s. c  om
 * @param driverClassLoader Class loader to be used to load the JDBC driver.
 * @return the <code>BasicDataSource</code> instance.
 * @throws SQLException If a database access error occurs.
 */
public static BasicDataSource createDataSource(final Dbcp dbcp, final ClassLoader driverClassLoader)
        throws SQLException {
    final BasicDataSource dataSource = new BasicDataSource();

    final Dbcp.Jdbc jdbc = dbcp.getJdbc();
    dataSource.setDriverClassName(jdbc.getDriverClassName());
    dataSource.setDriverClassLoader(driverClassLoader);

    dataSource.setUrl(jdbc.getUrl());

    dataSource.setUsername(jdbc.getUsername());
    dataSource.setPassword(jdbc.getPassword());

    final Dbcp.Default _default = dbcp.getDefault();
    if (_default != null && _default.getCatalog() != null)
        dataSource.setDefaultCatalog(_default.getCatalog());

    dataSource.setDefaultAutoCommit(
            _default == null || _default.getAutoCommit() == null || _default.getAutoCommit());
    dataSource.setDefaultReadOnly(_default != null && _default.getReadOnly() != null && _default.getReadOnly());
    if (_default != null && _default.getQueryTimeout() != null)
        dataSource.setDefaultQueryTimeout(_default.getQueryTimeout());

    if (_default != null && _default.getTransactionIsolation() != null) {
        if ("NONE".equals(_default.getTransactionIsolation()))
            dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE);
        else if ("READ_COMMITTED".equals(_default.getTransactionIsolation()))
            dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        else if ("READ_UNCOMMITTED".equals(_default.getTransactionIsolation()))
            dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
        else if ("REPEATABLE_READ".equals(_default.getTransactionIsolation()))
            dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        else if ("SERIALIZABLE".equals(_default.getTransactionIsolation()))
            dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        else
            throw new UnsupportedOperationException(
                    "Unsupported transaction isolation: " + _default.getTransactionIsolation());
    }

    final Dbcp.Connection connection = dbcp.getConnection();
    if (connection != null) {
        if (connection.getProperties() != null)
            for (final Dbcp.Connection.Properties.Property property : connection.getProperties().getProperty())
                if (property.getName() != null && property.getValue() != null)
                    dataSource.addConnectionProperty(property.getName(), property.getValue());

        if (connection.getInitSqls() != null) {
            final List<String> initSqls = new ArrayList<>();
            for (final String initSql : connection.getInitSqls().getInitSql())
                initSqls.add(initSql);

            dataSource.setConnectionInitSqls(initSqls);
        }
    }

    final Dbcp.Size size = dbcp.getSize();
    dataSource.setInitialSize(size == null || size.getInitialSize() == null ? 0 : size.getInitialSize());
    dataSource.setMaxTotal(size == null || size.getMaxTotal() == null ? 8
            : INDEFINITE.equals(size.getMaxTotal()) ? -1 : Integer.parseInt(size.getMaxTotal()));
    dataSource.setMaxIdle(size == null || size.getMaxIdle() == null ? 8
            : INDEFINITE.equals(size.getMaxIdle()) ? -1 : Integer.parseInt(size.getMaxIdle()));
    dataSource.setMinIdle(size == null || size.getMinIdle() == null ? 9 : size.getMinIdle());
    if (size == null || size.getMaxOpenPreparedStatements() == null
            || INDEFINITE.equals(size.getMaxOpenPreparedStatements())) {
        dataSource.setPoolPreparedStatements(false);
    } else {
        dataSource.setPoolPreparedStatements(true);
        dataSource.setMaxOpenPreparedStatements(Integer.parseInt(size.getMaxOpenPreparedStatements()));
    }

    final Dbcp.Pool pool = dbcp.getPool();
    if (pool == null || pool.getQueue() == null || "lifo".equals(pool.getQueue()))
        dataSource.setLifo(true);
    else if ("fifo".equals(pool.getQueue()))
        dataSource.setLifo(false);
    else
        throw new UnsupportedOperationException("Unsupported queue spec: " + pool.getQueue());

    dataSource.setCacheState(pool != null && pool.getCacheState() != null && pool.getCacheState());
    dataSource.setMaxWaitMillis(
            pool == null || pool.getMaxWait() != null || INDEFINITE.equals(pool.getMaxWait()) ? -1
                    : Long.parseLong(pool.getMaxWait()));
    dataSource.setMaxConnLifetimeMillis(pool == null || pool.getMaxConnectionLifetime() == null
            || INDEFINITE.equals(pool.getMaxConnectionLifetime()) ? 0
                    : Long.parseLong(pool.getMaxConnectionLifetime()));
    dataSource.setEnableAutoCommitOnReturn(_default == null || pool.getEnableAutoCommitOnReturn() == null
            || pool.getEnableAutoCommitOnReturn());
    dataSource.setRollbackOnReturn(
            pool == null || pool.getRollbackOnReturn() == null || pool.getRollbackOnReturn());
    if (pool != null && pool.getRemoveAbandoned() != null) {
        if ("borrow".equals(pool.getRemoveAbandoned().getOn()))
            dataSource.setRemoveAbandonedOnBorrow(true);
        else if ("maintenance".equals(pool.getRemoveAbandoned().getOn()))
            dataSource.setRemoveAbandonedOnMaintenance(true);
        else
            throw new UnsupportedOperationException(
                    "Unsupported remove abandoned spec: " + pool.getRemoveAbandoned().getOn());

        dataSource.setRemoveAbandonedTimeout(pool.getRemoveAbandoned().getTimeout());
    }

    dataSource.setAbandonedUsageTracking(
            pool != null && pool.getAbandonedUsageTracking() != null && pool.getAbandonedUsageTracking());
    dataSource.setAccessToUnderlyingConnectionAllowed(
            pool != null && pool.getAllowAccessToUnderlyingConnection() != null
                    && pool.getAllowAccessToUnderlyingConnection());

    final Dbcp.Pool.Eviction evictor = pool != null && pool.getEviction() != null ? pool.getEviction() : null;
    if (evictor != null) {
        dataSource.setTimeBetweenEvictionRunsMillis(evictor.getTimeBetweenRuns());
        dataSource.setNumTestsPerEvictionRun(evictor.getNumTestsPerRun());
        dataSource.setMinEvictableIdleTimeMillis(
                evictor.getMinIdleTime() == null ? 1800000 : evictor.getMinIdleTime());
        dataSource.setSoftMinEvictableIdleTimeMillis(
                evictor.getSoftMinIdleTime() == null || INDEFINITE.equals(evictor.getSoftMinIdleTime()) ? -1
                        : Long.parseLong(evictor.getSoftMinIdleTime()));
        if (evictor.getPolicyClassName() != null)
            dataSource.setEvictionPolicyClassName(evictor.getPolicyClassName());
    }

    final Dbcp.Validation validation = dbcp.getValidation();
    if (validation != null && validation.getQuery() != null)
        dataSource.setValidationQuery(validation.getQuery());

    dataSource.setTestOnBorrow(
            validation == null || validation.getTestOnBorrow() == null || validation.getTestOnBorrow());
    dataSource.setTestOnReturn(
            validation != null && validation.getTestOnReturn() != null && validation.getTestOnReturn());
    dataSource.setTestWhileIdle(
            validation != null && validation.getTestWhileIdle() != null && validation.getTestWhileIdle());
    if (validation != null && validation.getFastFail() != null) {
        dataSource.setFastFailValidation(true);
        if (validation.getFastFail().getDisconnectionSqlCodes() != null)
            dataSource.setDisconnectionSqlCodes(
                    Arrays.asList(validation.getFastFail().getDisconnectionSqlCodes().split(" ")));
    }

    final Dbcp.Logging logging = dbcp.getLogging();
    if (logging != null) {
        final Logger logger = LoggerFactory.getLogger(DataSources.class);
        final LoggerPrintWriter loggerPrintWriter = new LoggerPrintWriter(logger,
                Level.valueOf(logging.getLevel().toString()));
        dataSource.setLogWriter(loggerPrintWriter);
        dataSource.setLogExpiredConnections(logging.isLogExpiredConnections());
        if (logging.isLogAbandoned()) {
            dataSource.setAbandonedLogWriter(loggerPrintWriter);
            dataSource.setLogAbandoned(true);
        }
    }

    return dataSource;
}

From source file:org.everit.osgi.jdbc.commons.dbcp.internal.Util.java

public static void applyPropertiesOnBasicDataSource(final BasicDataSource basicDataSource,
        final Map<String, Object> componentProperties) {

    setPropertyIfNotNull(basicDataSource, componentProperties,
            DSFConstants.PROP_ACCESS_TO_UNDERLYING_CONNECTION_ALLOWED, boolean.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_DEFAULT_AUTO_COMMIT,
            Boolean.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_DEFAULT_CATALOG, String.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_DEFAULT_READ_ONLY,
            Boolean.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_DEFAULT_QUERY_TIMEOUT,
            Integer.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_INITIAL_SIZE, int.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_LOG_ABANDONED, boolean.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_MAX_TOTAL, int.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_MAX_IDLE, int.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_MAX_OPEN_PREPARED_STATEMENTS,
            int.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_MAX_WAIT_MILLIS, long.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_MIN_EVICTABLE_IDLE_TIME_MILLIS,
            long.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_MIN_IDLE, int.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_NUM_TESTS_PER_EVICTION_RUN,
            int.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_POOL_PREPARED_STATEMENTS,
            boolean.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_REMOVE_ABANDONED_ON_BORROW,
            boolean.class);

    setPropertyIfNotNull(basicDataSource, componentProperties,
            DSFConstants.PROP_REMOVE_ABANDONED_ON_MAINTENANCE, boolean.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_REMOVE_ABANDONED_TIMEOUT,
            int.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_TEST_ON_BORROW, boolean.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_TEST_ON_RETURN, boolean.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_TEST_WHILE_IDLE,
            boolean.class);

    setPropertyIfNotNull(basicDataSource, componentProperties,
            DSFConstants.PROP_TIME_BETWEEN_EVICTION_RUNS_MILLIS, long.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_VALIDATION_QUERY,
            String.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_VALIDATION_QUERY_TIMEOUT,
            int.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_LIFO, boolean.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_CACHE_STATE, boolean.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_TEST_ON_CREATE, boolean.class);

    setPropertyIfNotNull(basicDataSource, componentProperties,
            DSFConstants.PROP_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS, long.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_MAX_CONN_LIFETIME_MILLIS,
            long.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_JMX_NAME, String.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_ENABLE_AUTO_COMMIT_ON_RETURN,
            boolean.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_ROLLBACK_ON_RETURN,
            boolean.class);

    setPropertyIfNotNull(basicDataSource, componentProperties, DSFConstants.PROP_ABANDONED_USAGE_TRACKING,
            boolean.class);

    Object connectionInitSqlsObject = componentProperties.get(DSFConstants.PROP_CONNECTION_INIT_SQLS);
    if (connectionInitSqlsObject != null) {
        if (!(connectionInitSqlsObject instanceof Collection)) {
            throw new RuntimeException("Expected type of " + DSFConstants.PROP_CONNECTION_INIT_SQLS
                    + "' is Collection but " + connectionInitSqlsObject.getClass() + " provided");
        }/*from  www.  ja va2s.c o m*/

        @SuppressWarnings("unchecked")
        Collection<String> connectionInitSqls = (Collection<String>) connectionInitSqlsObject;
        basicDataSource.setConnectionInitSqls(connectionInitSqls);
    }

    Object transactionIsolationObject = componentProperties
            .get(DSFConstants.PROP_DEFAULT_TRANSACTION_ISOLATION);
    if (transactionIsolationObject != null) {
        if (DSFConstants.VALUE_READ_COMMITED.equals(transactionIsolationObject)) {
            basicDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        } else if (DSFConstants.VALUE_READ_UNCOMMITTED.equals(transactionIsolationObject)) {
            basicDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
        } else if (DSFConstants.VALUE_REPEATABLE_READ.equals(transactionIsolationObject)) {
            basicDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        } else if (DSFConstants.VALUE_SERIALIZABLE.equals(transactionIsolationObject)) {
            basicDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        } else if (DSFConstants.VALUE_TRANSACTION_NONE.equals(transactionIsolationObject)) {
            basicDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE);
        } else {
            throw new RuntimeException("Unknown value '" + transactionIsolationObject
                    + "' for the property key '" + DSFConstants.PROP_DEFAULT_TRANSACTION_ISOLATION
                    + " during datasource configuration.");
        }

    }
}

From source file:org.everit.persistence.jdbc.commons.dbcp.ecm.internal.Util.java

private static void applyTransactionIsolationPropertyOnBasicDataSource(final BasicDataSource basicDataSource,
        final Object transactionIsolationObject) {
    if (transactionIsolationObject != null) {
        if (DSFConstants.VALUE_READ_COMMITED.equals(transactionIsolationObject)) {
            basicDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        } else if (DSFConstants.VALUE_READ_UNCOMMITTED.equals(transactionIsolationObject)) {
            basicDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
        } else if (DSFConstants.VALUE_REPEATABLE_READ.equals(transactionIsolationObject)) {
            basicDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        } else if (DSFConstants.VALUE_SERIALIZABLE.equals(transactionIsolationObject)) {
            basicDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        } else if (DSFConstants.VALUE_TRANSACTION_NONE.equals(transactionIsolationObject)) {
            basicDataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE);
        } else {//  ww  w. ja  va  2  s .  c o m
            throw new RuntimeException("Unknown value '" + transactionIsolationObject
                    + "' for the property key '" + DSFConstants.ATTR_DEFAULT_TRANSACTION_ISOLATION
                    + " during datasource configuration.");
        }
    }
}

From source file:org.nuxeo.runtime.datasource.BasicManagedDataSourceFactory.java

/**
 * Creates and configures a {@link BasicManagedDataSource} instance based on
 * the given properties.// www.j  a  va 2s.  co m
 *
 * @param properties the datasource configuration properties
 * @throws Exception if an error occurs creating the data source
 */
public static DataSource createDataSource(Properties properties) throws Exception {
    BasicManagedDataSource dataSource = new BasicManagedDataSource();

    String value = properties.getProperty(PROP_DEFAULTAUTOCOMMIT);
    if (value != null) {
        dataSource.setDefaultAutoCommit(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_DEFAULTREADONLY);
    if (value != null) {
        dataSource.setDefaultReadOnly(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_DEFAULTTRANSACTIONISOLATION);
    if (value != null) {
        int level = UNKNOWN_TRANSACTIONISOLATION;
        if ("NONE".equalsIgnoreCase(value)) {
            level = Connection.TRANSACTION_NONE;
        } else if ("READ_COMMITTED".equalsIgnoreCase(value)) {
            level = Connection.TRANSACTION_READ_COMMITTED;
        } else if ("READ_UNCOMMITTED".equalsIgnoreCase(value)) {
            level = Connection.TRANSACTION_READ_UNCOMMITTED;
        } else if ("REPEATABLE_READ".equalsIgnoreCase(value)) {
            level = Connection.TRANSACTION_REPEATABLE_READ;
        } else if ("SERIALIZABLE".equalsIgnoreCase(value)) {
            level = Connection.TRANSACTION_SERIALIZABLE;
        } else {
            try {
                level = Integer.parseInt(value);
            } catch (NumberFormatException e) {
                System.err.println("Could not parse defaultTransactionIsolation: " + value);
                System.err.println("WARNING: defaultTransactionIsolation not set");
                System.err.println("using default value of database driver");
                level = UNKNOWN_TRANSACTIONISOLATION;
            }
        }
        dataSource.setDefaultTransactionIsolation(level);
    }

    value = properties.getProperty(PROP_DEFAULTCATALOG);
    if (value != null) {
        dataSource.setDefaultCatalog(value);
    }

    value = properties.getProperty(PROP_DRIVERCLASSNAME);
    if (value != null) {
        dataSource.setDriverClassName(value);
    }

    value = properties.getProperty(PROP_MAXACTIVE);
    if (value != null) {
        dataSource.setMaxActive(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MAXIDLE);
    if (value != null) {
        dataSource.setMaxIdle(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MINIDLE);
    if (value != null) {
        dataSource.setMinIdle(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_INITIALSIZE);
    if (value != null) {
        dataSource.setInitialSize(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MAXWAIT);
    if (value != null) {
        dataSource.setMaxWait(Long.parseLong(value));
    }

    value = properties.getProperty(PROP_TESTONBORROW);
    if (value != null) {
        dataSource.setTestOnBorrow(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_TESTONRETURN);
    if (value != null) {
        dataSource.setTestOnReturn(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_TIMEBETWEENEVICTIONRUNSMILLIS);
    if (value != null) {
        dataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(value));
    }

    value = properties.getProperty(PROP_NUMTESTSPEREVICTIONRUN);
    if (value != null) {
        dataSource.setNumTestsPerEvictionRun(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_MINEVICTABLEIDLETIMEMILLIS);
    if (value != null) {
        dataSource.setMinEvictableIdleTimeMillis(Long.parseLong(value));
    }

    value = properties.getProperty(PROP_TESTWHILEIDLE);
    if (value != null) {
        dataSource.setTestWhileIdle(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_PASSWORD);
    if (value != null) {
        dataSource.setPassword(value);
    }

    value = properties.getProperty(PROP_URL);
    if (value != null) {
        dataSource.setUrl(value);
    }

    value = properties.getProperty(PROP_USERNAME);
    if (value != null) {
        dataSource.setUsername(value);
    }

    value = properties.getProperty(PROP_VALIDATIONQUERY);
    if (value != null) {
        dataSource.setValidationQuery(value);
    }

    value = properties.getProperty(PROP_VALIDATIONQUERY_TIMEOUT);
    if (value != null) {
        dataSource.setValidationQueryTimeout(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED);
    if (value != null) {
        dataSource.setAccessToUnderlyingConnectionAllowed(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_REMOVEABANDONED);
    if (value != null) {
        dataSource.setRemoveAbandoned(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_REMOVEABANDONEDTIMEOUT);
    if (value != null) {
        dataSource.setRemoveAbandonedTimeout(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_LOGABANDONED);
    if (value != null) {
        dataSource.setLogAbandoned(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_POOLPREPAREDSTATEMENTS);
    if (value != null) {
        dataSource.setPoolPreparedStatements(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(PROP_MAXOPENPREPAREDSTATEMENTS);
    if (value != null) {
        dataSource.setMaxOpenPreparedStatements(Integer.parseInt(value));
    }

    value = properties.getProperty(PROP_INITCONNECTIONSQLS);
    if (value != null) {
        StringTokenizer tokenizer = new StringTokenizer(value, ";");
        dataSource.setConnectionInitSqls(Collections.list(tokenizer));
    }

    value = properties.getProperty(PROP_CONNECTIONPROPERTIES);
    if (value != null) {
        Properties p = getProperties(value);
        Enumeration<?> e = p.propertyNames();
        while (e.hasMoreElements()) {
            String propertyName = (String) e.nextElement();
            dataSource.addConnectionProperty(propertyName, p.getProperty(propertyName));
        }
    }

    // Managed: initialize XADataSource

    value = properties.getProperty(PROP_XADATASOURCE);
    if (value != null) {
        Class<?> xaDataSourceClass;
        try {
            xaDataSourceClass = Class.forName(value);
        } catch (Throwable t) {
            throw (SQLException) new SQLException("Cannot load XA data source class '" + value + "'")
                    .initCause(t);
        }
        XADataSource xaDataSource;
        try {
            xaDataSource = (XADataSource) xaDataSourceClass.newInstance();
        } catch (Throwable t) {
            throw (SQLException) new SQLException("Cannot create XA data source of class '" + value + "'")
                    .initCause(t);
        }
        dataSource.setXaDataSourceInstance(xaDataSource);
    }

    // DBCP-215
    // Trick to make sure that initialSize connections are created
    if (dataSource.getInitialSize() > 0) {
        dataSource.getLogWriter();
    }

    // Return the configured DataSource instance
    return dataSource;
}

From source file:org.apache.synapse.config.xml.AbstractDBMediatorFactory.java

/**
 * Create a custom DataSource using the specified properties and Apache DBCP
 * @param pool the toplevel 'pool' element that holds DataSource information
 * @param mediator the mediator to store properties for serialization
 * @return a DataSource created using specified properties
 *///  w w  w.j  a  v a  2  s  .  c o  m
private DataSource createCustomDataSource(OMElement pool, AbstractDBMediator mediator) {

    BasicDataSource ds = new BasicDataSource();

    // load the minimum required properties
    ds.setDriverClassName(getValue(pool, DRIVER_Q));
    ds.setUsername(getValue(pool, USER_Q));
    ds.setPassword(getValue(pool, PASS_Q));
    ds.setUrl(getValue(pool, URL_Q));

    //save loaded properties for later
    mediator.addDataSourceProperty(DRIVER_Q, getValue(pool, DRIVER_Q));
    mediator.addDataSourceProperty(URL_Q, getValue(pool, URL_Q));
    mediator.addDataSourceProperty(USER_Q, getValue(pool, USER_Q));
    mediator.addDataSourceProperty(PASS_Q, getValue(pool, PASS_Q));

    Iterator props = pool.getChildrenWithName(PROP_Q);
    while (props.hasNext()) {

        OMElement prop = (OMElement) props.next();
        String name = prop.getAttribute(ATT_NAME).getAttributeValue();
        String value = prop.getAttribute(ATT_VALUE).getAttributeValue();
        // save property for later
        mediator.addDataSourceProperty(name, value);

        if ("autocommit".equals(name)) {
            if ("true".equals(value)) {
                ds.setDefaultAutoCommit(true);
            } else if ("false".equals(value)) {
                ds.setDefaultAutoCommit(false);
            }
        } else if ("isolation".equals(name)) {
            try {
                if ("Connection.TRANSACTION_NONE".equals(value)) {
                    ds.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE);
                } else if ("Connection.TRANSACTION_READ_COMMITTED".equals(value)) {
                    ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
                } else if ("Connection.TRANSACTION_READ_UNCOMMITTED".equals(value)) {
                    ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
                } else if ("Connection.TRANSACTION_REPEATABLE_READ".equals(value)) {
                    ds.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
                } else if ("Connection.TRANSACTION_SERIALIZABLE".equals(value)) {
                    ds.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
                }
            } catch (NumberFormatException ignore) {
            }
        } else if ("initialsize".equals(name)) {
            try {
                ds.setInitialSize(Integer.parseInt(value));
            } catch (NumberFormatException ignore) {
            }
        } else if ("maxactive".equals(name)) {
            try {
                ds.setMaxActive(Integer.parseInt(value));
            } catch (NumberFormatException ignore) {
            }
        } else if ("maxidle".equals(name)) {
            try {
                ds.setMaxIdle(Integer.parseInt(value));
            } catch (NumberFormatException ignore) {
            }
        } else if ("maxopenstatements".equals(name)) {
            try {
                ds.setMaxOpenPreparedStatements(Integer.parseInt(value));
            } catch (NumberFormatException ignore) {
            }
        } else if ("maxwait".equals(name)) {
            try {
                ds.setMaxWait(Long.parseLong(value));
            } catch (NumberFormatException ignore) {
            }
        } else if ("minidle".equals(name)) {
            try {
                ds.setMinIdle(Integer.parseInt(value));
            } catch (NumberFormatException ignore) {
            }
        } else if ("poolstatements".equals(name)) {
            if ("true".equals(value)) {
                ds.setPoolPreparedStatements(true);
            } else if ("false".equals(value)) {
                ds.setPoolPreparedStatements(false);
            }
        } else if ("testonborrow".equals(name)) {
            if ("true".equals(value)) {
                ds.setTestOnBorrow(true);
            } else if ("false".equals(value)) {
                ds.setTestOnBorrow(false);
            }
        } else if ("testonreturn".equals(name)) {
            if ("true".equals(value)) {
                ds.setTestOnReturn(true);
            } else if ("false".equals(value)) {
                ds.setTestOnReturn(false);
            }
        } else if ("testwhileidle".equals(name)) {
            if ("true".equals(value)) {
                ds.setTestWhileIdle(true);
            } else if ("false".equals(value)) {
                ds.setTestWhileIdle(false);
            }
        } else if ("validationquery".equals(name)) {
            ds.setValidationQuery(value);
        }
    }
    return ds;
}

From source file:com.glaf.core.jdbc.connection.TomcatJdbcConnectionProvider.java

protected void parsePoolProperties(PoolConfiguration poolProperties, Properties properties) {

    String value = null;// ww w  .j  a  v  a2  s.  c om

    value = properties.getProperty(ConnectionConstants.PROP_DEFAULTAUTOCOMMIT);
    if (value != null) {
        poolProperties.setDefaultAutoCommit(Boolean.valueOf(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_DEFAULTREADONLY);
    if (value != null) {
        poolProperties.setDefaultReadOnly(Boolean.valueOf(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_DEFAULTTRANSACTIONISOLATION);
    if (value != null) {
        int level = ConnectionConstants.UNKNOWN_TRANSACTIONISOLATION;
        if ("NONE".equalsIgnoreCase(value)) {
            level = Connection.TRANSACTION_NONE;
        } else if ("READ_COMMITTED".equalsIgnoreCase(value)) {
            level = Connection.TRANSACTION_READ_COMMITTED;
        } else if ("READ_UNCOMMITTED".equalsIgnoreCase(value)) {
            level = Connection.TRANSACTION_READ_UNCOMMITTED;
        } else if ("REPEATABLE_READ".equalsIgnoreCase(value)) {
            level = Connection.TRANSACTION_REPEATABLE_READ;
        } else if ("SERIALIZABLE".equalsIgnoreCase(value)) {
            level = Connection.TRANSACTION_SERIALIZABLE;
        } else {
            try {
                level = Integer.parseInt(value);
            } catch (NumberFormatException e) {
                System.err.println("Could not parse defaultTransactionIsolation: " + value);
                System.err.println("WARNING: defaultTransactionIsolation not set");
                System.err.println("using default value of database driver");
                level = ConnectionConstants.UNKNOWN_TRANSACTIONISOLATION;
            }
        }
        poolProperties.setDefaultTransactionIsolation(level);
    }

    value = properties.getProperty(ConnectionConstants.PROP_DEFAULTCATALOG);
    if (value != null) {
        poolProperties.setDefaultCatalog(value);
    }

    value = properties.getProperty(ConnectionConstants.PROP_MAXACTIVE);
    if (value != null && StringUtils.isNumeric(value)) {
        poolProperties.setMaxActive(Integer.parseInt(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_MAXIDLE);
    if (value != null && StringUtils.isNumeric(value)) {
        poolProperties.setMaxIdle(Integer.parseInt(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_MINIDLE);
    if (value != null && StringUtils.isNumeric(value)) {
        poolProperties.setMinIdle(Integer.parseInt(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_INITIALSIZE);
    if (value != null && StringUtils.isNumeric(value)) {
        poolProperties.setInitialSize(Integer.parseInt(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_MAXWAIT);
    if (value != null && StringUtils.isNumeric(value)) {
        poolProperties.setMaxWait(Integer.parseInt(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_TESTONBORROW);
    if (value != null) {
        poolProperties.setTestOnBorrow(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(ConnectionConstants.PROP_TESTONRETURN);
    if (value != null) {
        poolProperties.setTestOnReturn(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(ConnectionConstants.PROP_TESTONCONNECT);
    if (value != null) {
        poolProperties.setTestOnConnect(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(ConnectionConstants.PROP_TIMEBETWEENEVICTIONRUNSMILLIS);
    if (value != null && StringUtils.isNumeric(value)) {
        poolProperties.setTimeBetweenEvictionRunsMillis(Integer.parseInt(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_NUMTESTSPEREVICTIONRUN);
    if (value != null && StringUtils.isNumeric(value)) {
        poolProperties.setNumTestsPerEvictionRun(Integer.parseInt(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_MINEVICTABLEIDLETIMEMILLIS);
    if (value != null && StringUtils.isNumeric(value)) {
        poolProperties.setMinEvictableIdleTimeMillis(Integer.parseInt(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_TESTWHILEIDLE);
    if (value != null) {
        poolProperties.setTestWhileIdle(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(ConnectionConstants.PROP_VALIDATOR_CLASS_NAME);
    if (value != null) {
        poolProperties.setValidatorClassName(value);
    }

    value = properties.getProperty(ConnectionConstants.PROP_VALIDATIONINTERVAL);
    if (value != null && StringUtils.isNumeric(value)) {
        poolProperties.setValidationInterval(Long.parseLong(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_ACCESSTOUNDERLYINGCONNECTIONALLOWED);
    if (value != null) {
        poolProperties.setAccessToUnderlyingConnectionAllowed(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(ConnectionConstants.PROP_REMOVEABANDONED);
    if (value != null) {
        poolProperties.setRemoveAbandoned(Boolean.valueOf(value).booleanValue());
    }

    value = properties.getProperty(ConnectionConstants.PROP_REMOVEABANDONEDTIMEOUT);
    if (value != null && StringUtils.isNumeric(value)) {
        poolProperties.setRemoveAbandonedTimeout(Integer.parseInt(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_LOGABANDONED);
    if (value != null) {
        poolProperties.setLogAbandoned(Boolean.valueOf(value).booleanValue());
    }

    if (poolProperties.getUsername() != null) {
        poolProperties.getDbProperties().setProperty("user", poolProperties.getUsername());
    }
    if (poolProperties.getPassword() != null) {
        poolProperties.getDbProperties().setProperty("password", poolProperties.getPassword());
    }

    value = properties.getProperty(ConnectionConstants.PROP_INITSQL);
    if (value != null) {
        poolProperties.setInitSQL(value);
    }

    value = properties.getProperty(ConnectionConstants.PROP_INTERCEPTORS);
    if (value != null) {
        poolProperties.setJdbcInterceptors(value);
    }

    value = properties.getProperty(ConnectionConstants.PROP_JMX_ENABLED);
    if (value != null) {
        poolProperties.setJmxEnabled(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_FAIR_QUEUE);
    if (value != null) {
        poolProperties.setFairQueue(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_USE_EQUALS);
    if (value != null) {
        poolProperties.setUseEquals(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_ABANDONWHENPERCENTAGEFULL);
    if (value != null && StringUtils.isNumeric(value)) {
        poolProperties.setAbandonWhenPercentageFull(Integer.parseInt(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_MAXAGE);
    if (value != null && StringUtils.isNumeric(value)) {
        poolProperties.setMaxAge(Long.parseLong(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_USE_CON_LOCK);
    if (value != null) {
        poolProperties.setUseLock(Boolean.parseBoolean(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_SUSPECT_TIMEOUT);
    if (value != null && StringUtils.isNumeric(value)) {
        poolProperties.setSuspectTimeout(Integer.parseInt(value));
    }

    value = properties.getProperty(ConnectionConstants.PROP_ALTERNATE_USERNAME_ALLOWED);
    if (value != null) {
        poolProperties.setAlternateUsernameAllowed(Boolean.parseBoolean(value));
    }

}

From source file:org.jxstar.dao.pool.PooledConnection.java

/**
 * ???JDBC//w w w . j  ava 2s .c om
 * Connection.TRANSACTION_NONE
 *       ????
 * Connection.TRANSACTION_READ_UNCOMMITTED
 *       ?????????
 * Connection.TRANSACTION_READ_COMMITTED(?)
 *       ????????? 
 * Connection.TRANSACTION_REPEATABLE_READ
 *       ?????????
 * Connection.TRANSACTION_SERIALIZABLE
 *       ????????
 * 
 * @param sTranLevel
 * @return
 */
private int getTranLevelConstant(String sTranLevel) {
    if (sTranLevel == null || sTranLevel.length() == 0) {
        return Connection.TRANSACTION_READ_COMMITTED;
    }

    String sTmpLevel = sTranLevel.toUpperCase();
    if (sTmpLevel.equals("TRANSACTION_NONE")) {
        return Connection.TRANSACTION_NONE;
    } else if (sTmpLevel.equals("TRANSACTION_READ_UNCOMMITTED")) {
        return Connection.TRANSACTION_READ_UNCOMMITTED;
    } else if (sTmpLevel.equals("TRANSACTION_READ_COMMITTED")) {
        return Connection.TRANSACTION_READ_COMMITTED;
    } else if (sTmpLevel.equals("TRANSACTION_REPEATABLE_READ")) {
        return Connection.TRANSACTION_REPEATABLE_READ;
    } else if (sTmpLevel.equals("TRANSACTION_SERIALIZABLE")) {
        return Connection.TRANSACTION_SERIALIZABLE;
    }

    return Connection.TRANSACTION_READ_COMMITTED;
}

From source file:org.pentaho.di.trans.dataservice.jdbc.ThinConnectionTest.java

@Test
public void testUnusedProperties() throws Exception {
    connection.setSchema("schema ");
    assertThat(connection.getSchema(), nullValue());

    connection.setCatalog("catalog");
    assertThat(connection.getCatalog(), nullValue());

    connection.close();//w ww  .  jav a2s  .c o m
    assertThat(connection.isClosed(), is(false));

    connection.setAutoCommit(false);
    assertThat(connection.getAutoCommit(), is(true));

    connection.setReadOnly(false);
    assertThat(connection.isReadOnly(), is(true));

    connection.setClientInfo(properties);
    assertThat(connection.getClientInfo(), anEmptyMap());

    assertThat(connection.getTransactionIsolation(), is(Connection.TRANSACTION_NONE));

    connection.setHoldability(RowsResultSet.CLOSE_CURSORS_AT_COMMIT);
    assertThat(connection.getHoldability(), equalTo(RowsResultSet.CLOSE_CURSORS_AT_COMMIT));
}

From source file:org.apache.jena.jdbc.remote.connections.RemoteEndpointConnection.java

@Override
protected void checkTransactionIsolation(int level) throws SQLException {
    switch (level) {
    case Connection.TRANSACTION_NONE:
        return;/*from  w  ww  .j  a v  a 2  s  .  c  o  m*/
    default:
        throw new SQLFeatureNotSupportedException(
                "Transactions are not supported for remote endpoint backed connections");
    }
}

From source file:org.wso2.carbon.datasource.ui.DataSourceManagementHelper.java

public static String toStringIsolation(int isolation) {
    switch (isolation) {
    case Connection.TRANSACTION_NONE: {
        return "TRANSACTION_NONE";
    }/*  ww w .  ja  v  a  2 s . c  om*/
    case Connection.TRANSACTION_READ_COMMITTED: {
        return "TRANSACTION_READ_COMMITTED";
    }
    case Connection.TRANSACTION_READ_UNCOMMITTED: {
        return "TRANSACTION_READ_UNCOMMITTED";
    }
    case Connection.TRANSACTION_REPEATABLE_READ: {
        return "TRANSACTION_REPEATABLE_READ";
    }
    case Connection.TRANSACTION_SERIALIZABLE: {
        return "TRANSACTION_SERIALIZABLE";
    }
    default: {
        return "TRANSACTION_UNKNOWN";
    }
    }
}