Example usage for org.springframework.jdbc CannotGetJdbcConnectionException CannotGetJdbcConnectionException

List of usage examples for org.springframework.jdbc CannotGetJdbcConnectionException CannotGetJdbcConnectionException

Introduction

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

Prototype

public CannotGetJdbcConnectionException(String msg, @Nullable SQLException ex) 

Source Link

Document

Constructor for CannotGetJdbcConnectionException.

Usage

From source file:com.alibaba.cobar.client.CobarSqlMapClientTemplate.java

protected Object executeWith(DataSource dataSource, SqlMapClientCallback action) {
    SqlMapSession session = getSqlMapClient().openSession();

    try {//from   w w  w .  j  av a2  s. c o m
        Connection springCon = null;
        boolean transactionAware = (dataSource instanceof TransactionAwareDataSourceProxy);

        // Obtain JDBC Connection to operate on...
        try {
            springCon = (transactionAware ? dataSource.getConnection()
                    : DataSourceUtils.doGetConnection(dataSource));
            session.setUserConnection(springCon);
        } catch (SQLException ex) {
            throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);
        }

        try {
            return action.doInSqlMapClient(session);
        } catch (SQLException ex) {
            throw new SQLErrorCodeSQLExceptionTranslator().translate("SqlMapClient operation", null, ex);
        } catch (Throwable t) {
            throw new UncategorizedCobarClientException(
                    "unknown excepton when performing data access operation.", t);
        } finally {
            try {
                if (springCon != null) {
                    if (transactionAware) {
                        springCon.close();
                    } else {
                        DataSourceUtils.doReleaseConnection(springCon, dataSource);
                    }
                }
            } catch (Throwable ex) {
                logger.debug("Could not close JDBC Connection", ex);
            }
        }
        // Processing finished - potentially session still to be closed.
    } finally {
        session.close();
    }
}

From source file:org.springframework.batch.item.database.IbatisBatchItemWriter.java

@SuppressWarnings("unchecked")
private List<BatchResult> execute(final List<? extends T> items) {
    // We always need to use a SqlMapSession, as we need to pass a Spring-managed
    // Connection (potentially transactional) in. This shouldn't be necessary if
    // we run against a TransactionAwareDataSourceProxy underneath, but unfortunately
    // we still need it to make iBATIS batch execution work properly: If iBATIS
    // doesn't recognize an existing transaction, it automatically executes the
    // batch for every single statement...

    SqlMapSession session = this.sqlMapClient.openSession();
    if (logger.isDebugEnabled()) {
        logger.debug("Opened SqlMapSession [" + session + "] for iBATIS operation");
    }/*from  w  w  w  .  j  a  v  a  2  s  .  c  o m*/
    Connection ibatisCon = null;

    try {
        Connection springCon = null;
        boolean transactionAware = (dataSource instanceof TransactionAwareDataSourceProxy);

        // Obtain JDBC Connection to operate on...
        try {
            ibatisCon = session.getCurrentConnection();
            if (ibatisCon == null) {
                springCon = (transactionAware ? dataSource.getConnection()
                        : DataSourceUtils.doGetConnection(dataSource));
                session.setUserConnection(springCon);
                if (logger.isDebugEnabled()) {
                    logger.debug("Obtained JDBC Connection [" + springCon + "] for iBATIS operation");
                }
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("Reusing JDBC Connection [" + ibatisCon + "] for iBATIS operation");
                }
            }
        } catch (SQLException ex) {
            throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);
        }

        // Execute given callback...
        try {
            session.startBatch();
            for (T item : items) {
                session.update(statementId, item);
            }
            try {
                return session.executeBatchDetailed();
            } catch (BatchException e) {
                throw e.getBatchUpdateException();
            }
        } catch (SQLException ex) {
            SQLExceptionTranslator sqlStateSQLExceptionTranslator;

            if (dataSource != null) {
                sqlStateSQLExceptionTranslator = new SQLStateSQLExceptionTranslator();
            } else {
                sqlStateSQLExceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource);
            }

            throw sqlStateSQLExceptionTranslator.translate("SqlMapClient operation", null, ex);
        }

        // Processing finished - potentially session still to be closed.
    } finally {
        // Only close SqlMapSession if we know we've actually opened it
        // at the present level.
        if (ibatisCon == null) {
            session.close();
        }
    }
}

From source file:org.springframework.jdbc.datasource.DataSourceUtils.java

/**
 * Obtain a Connection from the given DataSource. Translates SQLExceptions into
 * the Spring hierarchy of unchecked generic data access exceptions, simplifying
 * calling code and making any exception that is thrown more meaningful.
 * <p>Is aware of a corresponding Connection bound to the current thread, for example
 * when using {@link DataSourceTransactionManager}. Will bind a Connection to the
 * thread if transaction synchronization is active, e.g. when running within a
 * {@link org.springframework.transaction.jta.JtaTransactionManager JTA} transaction).
 * @param dataSource the DataSource to obtain Connections from
 * @return a JDBC Connection from the given DataSource
 * @throws org.springframework.jdbc.CannotGetJdbcConnectionException
 * if the attempt to get a Connection failed
 * @see #releaseConnection//from   www. j av a  2 s  . c o  m
 */
public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException {
    try {
        return doGetConnection(dataSource);
    } catch (SQLException ex) {
        throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection", ex);
    } catch (IllegalStateException ex) {
        throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection: " + ex.getMessage());
    }
}

From source file:org.springframework.jdbc.datasource.DriverManagerDataSource.java

/**
 * Set the JDBC driver class name. This driver will get initialized
 * on startup, registering itself with the JDK's DriverManager.
 * <p>Alternatively, consider initializing the JDBC driver yourself
 * before instantiating this DataSource.
 * @see Class#forName(String)//from w w w .j ava  2  s. com
 * @see java.sql.DriverManager#registerDriver(java.sql.Driver)
 */
public void setDriverClassName(String driverClassName) throws CannotGetJdbcConnectionException {
    if (!StringUtils.hasText(driverClassName)) {
        throw new IllegalArgumentException("driverClassName must not be empty");
    }
    this.driverClassName = driverClassName;
    try {
        ClassUtils.forName(this.driverClassName);
    } catch (ClassNotFoundException ex) {
        throw new CannotGetJdbcConnectionException(
                "Could not load JDBC driver class [" + this.driverClassName + "]", ex);
    }
    if (logger.isInfoEnabled()) {
        logger.info("Loaded JDBC driver: " + this.driverClassName);
    }
}