Example usage for org.springframework.jdbc.datasource DataSourceUtils getConnection

List of usage examples for org.springframework.jdbc.datasource DataSourceUtils getConnection

Introduction

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

Prototype

public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException 

Source Link

Document

Obtain a Connection from the given DataSource.

Usage

From source file:org.hsweb.web.datasource.dynamic.DynamicDataSourceSqlExecutorService.java

@Override
public Connection getConnection() {
    return DataSourceUtils.getConnection(dynamicDataSource.getActiveDataSource());
}

From source file:com.common.bean.SourceTemplate.java

/**
 * ?/*w w  w .  java 2 s . c  o  m*/
 * <p>
 * "???dataSource?,???"
 */
@Deprecated
public static Connection getConn() throws Exception {
    Connection conn = DataSourceUtils.getConnection((DataSource) getBean("localDataSource"));
    return conn;
}

From source file:net.mlw.vlh.adapter.jdbc.spring.util.SpringConnectionCreator.java

public Connection createConnection() throws SQLException {
    return DataSourceUtils.getConnection(getDataSource());
}

From source file:com.oreilly.springdata.jpa.AbstractIntegrationTest.java

/**
 * Populates the configured {@link DataSource} with data from {@code data.sql}.
 * /*from  www  .ja  va 2  s. c o  m*/
 * @throws SQLException
 */
@Before
public void populateDatabase() throws SQLException {

    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(new ClassPathResource("data.sql"));

    Connection connection = null;

    try {
        connection = DataSourceUtils.getConnection(dataSource);
        populator.populate(connection);
    } finally {
        if (connection != null) {
            DataSourceUtils.releaseConnection(connection, dataSource);
        }
    }
}

From source file:com.backend.test.repository.AbstractIntegrationTest.java

/**
 * Populates the configured {@link DataSource} with data from {@code data.sql}.
 * //from  w w w  . jav a  2s  . c o m
 * @throws SQLException
 */
@Before
public void populateDatabase() throws SQLException {

    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(new ClassPathResource("foodies.sql"));

    Connection connection = null;

    try {
        connection = DataSourceUtils.getConnection(dataSource);
        populator.populate(connection);
    } finally {
        if (connection != null) {
            DataSourceUtils.releaseConnection(connection, dataSource);
        }
    }
}

From source file:org.jdbi.v3.spring4.JdbiFactoryBean.java

/**
 * See {@link org.springframework.beans.factory.FactoryBean#getObject}
 *//*  w  ww  . j  a va 2  s .c  o  m*/
@Override
public Jdbi getObject() throws Exception {
    final Jdbi jdbi = Jdbi.create(() -> DataSourceUtils.getConnection(dataSource));

    if (autoInstallPlugins) {
        jdbi.installPlugins();
    }

    plugins.forEach(jdbi::installPlugin);

    globalDefines.forEach(jdbi::define);

    return jdbi;
}

From source file:fi.luontola.cqrshotel.framework.PsqlEventStore.java

@Override
public long saveEvents(UUID streamId, List<Event> newEvents, int expectedVersion) {
    try (Connection connection = DataSourceUtils.getConnection(dataSource)) {
        Array data = connection.createArrayOf("jsonb", serializeData(newEvents));
        Array metadata = connection.createArrayOf("jsonb", serializeMetadata(newEvents));

        long endPosition = jdbcTemplate.queryForObject(
                "SELECT save_events(:stream_id, :expected_version, :data, :metadata)",
                new MapSqlParameterSource().addValue("stream_id", streamId)
                        .addValue("expected_version", expectedVersion).addValue("data", data)
                        .addValue("metadata", metadata),
                Long.class);

        if (log.isTraceEnabled()) {
            for (int i = 0; i < newEvents.size(); i++) {
                int newVersion = expectedVersion + 1 + i;
                Event newEvent = newEvents.get(i);
                log.trace("Saved stream {} version {}: {}", streamId, newVersion, newEvent);
            }//from   w w w. j a  va  2  s  . com
        }
        return endPosition;

    } catch (UncategorizedSQLException e) {
        if (e.getCause() instanceof PSQLException) {
            ServerErrorMessage serverError = ((PSQLException) e.getCause()).getServerErrorMessage();
            Matcher m = OPTIMISTIC_LOCKING_FAILURE_MESSAGE.matcher(serverError.getMessage());
            if (m.matches()) {
                String currentVersion = m.group(1);
                throw new OptimisticLockingException("expected version " + expectedVersion + " but was "
                        + currentVersion + " for stream " + streamId, e);
            }
        }
        throw e;
    } catch (SQLException | JsonProcessingException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.dcache.chimera.DirectoryStreamImpl.java

DirectoryStreamImpl(FsInode dir, JdbcTemplate jdbc) {
    _jdbc = jdbc;//w w w  .  ja v  a 2 s  .c o m

    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs;
    try {
        connection = DataSourceUtils.getConnection(_jdbc.getDataSource());
        ps = connection.prepareStatement(QUERY);
        ps.setFetchSize(50);
        ps.setLong(1, dir.ino());
        ps.setLong(2, dir.ino());
        ps.setLong(3, dir.ino());
        rs = ps.executeQuery();
    } catch (SQLException ex) {
        JdbcUtils.closeStatement(ps);
        DataSourceUtils.releaseConnection(connection, _jdbc.getDataSource());
        throw _jdbc.getExceptionTranslator().translate("StatementExecution", QUERY, ex);
    }
    _connection = connection;
    _resultSet = rs;
    _statement = ps;
}

From source file:org.hsweb.web.mybatis.SpringApplication.java

@Bean
public SqlExecutor sqlExecutor(DataSource dataSource) throws SQLException {
    Connection connection = dataSource.getConnection();
    try {//www  . j  ava  2  s. c  o  m
        DataSourceHolder.install(dataSource, DatabaseType.fromJdbcUrl(connection.getMetaData().getURL()));
    } finally {
        connection.close();
    }
    return new AbstractJdbcSqlExecutor() {
        @Override
        public Connection getConnection() {
            return DataSourceUtils.getConnection(dataSource);
        }

        @Override
        public void releaseConnection(Connection connection) throws SQLException {
            DataSourceUtils.releaseConnection(connection, dataSource);
        }
    };
}

From source file:com.bluexml.side.Integration.alfresco.sql.synchronization.common.JdbcTransactionListener.java

private Connection getConnection() {
    Connection connection = (Connection) AlfrescoTransactionSupport.getResource(SYNCHRO_CONTEXT_KEY);
    if (connection == null) {
        if (logger.isDebugEnabled())
            logger.debug("Creating new connection for transaction with id "
                    + AlfrescoTransactionSupport.getTransactionId());
        connection = DataSourceUtils.getConnection(dataSource);
        try {//from w  w w .j  a  v a2 s .c  o m
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            logger.error("Cannot set autocommit mode on the connection");
            logger.debug(e);
        }
        AlfrescoTransactionSupport.bindResource(SYNCHRO_CONTEXT_KEY, connection);
        AlfrescoTransactionSupport.bindListener(this);
        if (logger.isDebugEnabled())
            logger.debug("Attached connection to transaction " + AlfrescoTransactionSupport.getTransactionId());
    }
    return connection;
}