/** * 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 */ 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()); } }
public void removeTenantInfoAtomic(final String kp, final String tenantId) { try { jt.update("DELETE FROM tenant_info WHERE kp=? AND tenant_id=?", kp, tenantId); } catch (CannotGetJdbcConnectionException e) { fatalLog.error("[db-error] " + e.toString(), e); throw e; } }
Throwable causeThrowable = cannotGetJdbcConnectionException.getCause(); throw new IllegalArgumentException(String.valueOf(causeThrowable).trim(), cannotGetJdbcConnectionException);
/** * Allows a developer to do a manual release of the connection, * this will cause a new connection to be obtained the next time a method is executed, * anything not yet committed for this connection will be rolled back and lost, * normally if you are using a DataSource which pools connections then you do not * need to worry about this too much, just do it at the end of your work unit * * @return true if the connection was closed */ public boolean closeConnection() { boolean success = false; try { Connection conn = getConnection(); try { conn.rollback(); } catch (Exception e) { // oh well, keep going } DataSourceUtils.doReleaseConnection(conn, getDataSource()); success = true; } catch (CannotGetJdbcConnectionException e) { logWarn("Could not close connection sucessfully: " + e.getMessage()); } catch (SQLException e) { logWarn("Could not close connection sucessfully: " + e.getMessage()); } return success; }
Throwable causeThrowable = cannotGetJdbcConnectionException.getCause(); throw new IllegalArgumentException(String.valueOf(causeThrowable).trim(), cannotGetJdbcConnectionException);
con = this.dataSource.getConnection(); if (con == null) { throw new CannotGetJdbcConnectionException("Failed to execute validation query: " + "DataSource returned null from getConnection(): " + this.dataSource); throw new CannotGetJdbcConnectionException( "Database has not started up within " + this.timeout + " seconds", latestEx);
public void removeTagByIdAtomic(long id) { try { jt.update("DELETE FROM config_tags_relation WHERE id=?", id); } catch (CannotGetJdbcConnectionException e) { fatalLog.error("[db-error] " + e.toString(), e); throw e; } }
/** * For DataAccessException * * @throws DataAccessException */ @ExceptionHandler(DataAccessException.class) public void handleDataAccessException(HttpServletResponse response, DataAccessException ex) throws DataAccessException { MetricsMonitor.getDbException().increment(); throw new CannotGetJdbcConnectionException(ex.getMessage()); }
/** * 更新md5 */ public void updateMd5(String dataId, String group, String tenant, String md5, Timestamp lastTime) { String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant; try { jt.update( "UPDATE config_info SET md5 = ? WHERE data_id=? AND group_id=? AND tenant_id=? AND gmt_modified=?", md5, dataId, group, tenantTmp, lastTime); } catch (CannotGetJdbcConnectionException e) { fatalLog.error("[db-error] " + e.toString(), e); throw e; } }
/** * 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 */ 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()); } }
public ConfigInfoWrapper queryConfigInfo(final String dataId, final String group, final String tenant) { String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant; try { return this.jt .queryForObject( "SELECT ID,data_id,group_id,tenant_id,app_name,content,gmt_modified,md5 FROM config_info WHERE data_id=? AND group_id=? AND tenant_id=?", new Object[] {dataId, group, tenantTmp}, CONFIG_INFO_WRAPPER_ROW_MAPPER); } catch (EmptyResultDataAccessException e) { return null; } catch (CannotGetJdbcConnectionException e) { fatalLog.error("[db-error] " + e.toString(), e); throw e; } }
/** * 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 */ public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException { try { return doGetConnection(dataSource); } catch (SQLException ex) { throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex); } }
/** * 根据dataId和group查询配置信息 */ public ConfigInfoBase findConfigInfoBase(final String dataId, final String group) { try { return this.jt .queryForObject( "SELECT ID,data_id,group_id,content FROM config_info WHERE data_id=? AND group_id=? AND " + "tenant_id=?", new Object[] {dataId, group, StringUtils.EMPTY}, CONFIG_INFO_BASE_ROW_MAPPER); } catch (EmptyResultDataAccessException e) { // 表明数据不存在, 返回null return null; } catch (CannotGetJdbcConnectionException e) { fatalLog.error("[db-error] " + e.toString(), e); throw e; } }
/** * For DataAccessException * * @throws DataAccessException */ @ExceptionHandler(DataAccessException.class) public void handleDataAccessException(HttpServletResponse response, DataAccessException ex) throws DataAccessException { MetricsMonitor.getDbException().increment(); throw new CannotGetJdbcConnectionException(ex.getMessage()); }
/** * 增加配置;数据库原子操作,最小sql动作,无业务封装 * * @param dataId dataId * @param group group * @param appName appName * @param date date */ private void addConfigSubAtomic(final String dataId, final String group, final String appName, final Timestamp date) { final String appNameTmp = appName == null ? "" : appName; try { jt.update( "INSERT INTO app_configdata_relation_subs(data_id,group_id,app_name,gmt_modified) VALUES(?,?,?,?)", dataId, group, appNameTmp, date); } catch (CannotGetJdbcConnectionException e) { fatalLog.error("[db-error] " + e.toString(), e); throw e; } }
throw new CannotGetJdbcConnectionException( "Database has not started up within " + this.timeout + " seconds", latestEx);
/** * 根据数据库主键ID查询配置信息 * * @param id * @return */ public ConfigInfo findConfigInfo(long id) { try { return this.jt .queryForObject( "SELECT ID,data_id,group_id,tenant_id,app_name,content FROM config_info WHERE ID=?", new Object[] {id}, CONFIG_INFO_ROW_MAPPER); } catch (EmptyResultDataAccessException e) { // 表明数据不存在 return null; } catch (CannotGetJdbcConnectionException e) { fatalLog.error("[db-error] " + e.toString(), e); throw e; } }
con = this.dataSource.getConnection(); if (con == null) { throw new CannotGetJdbcConnectionException("Failed to execute validation query: " + "DataSource returned null from getConnection(): " + this.dataSource); throw new CannotGetJdbcConnectionException( "Database has not started up within " + this.timeout + " seconds", latestEx);
public User findUserByUsername(String username) { String sql = "SELECT username,password FROM users WHERE username=? "; try { return this.jt.queryForObject(sql, new Object[] {username}, USER_ROW_MAPPER); } catch (CannotGetJdbcConnectionException e) { fatalLog.error("[db-error] " + e.toString(), e); throw e; } catch (EmptyResultDataAccessException e) { return null; } catch (Exception e) { fatalLog.error("[db-other-error]" + e.getMessage(), e); throw new RuntimeException(e); } }
private List<RequestDepository> fetchConnectionsAndDepositForLaterUse( List<ConcurrentRequest> requests) { List<RequestDepository> depos = new ArrayList<RequestDepository>(); for (ConcurrentRequest request : requests) { DataSource dataSource = request.getDataSource(); Connection springCon = null; boolean transactionAware = (dataSource instanceof TransactionAwareDataSourceProxy); try { springCon = (transactionAware ? dataSource.getConnection() : DataSourceUtils .doGetConnection(dataSource)); } catch (SQLException ex) { throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex); } RequestDepository depo = new RequestDepository(); depo.setOriginalRequest(request); depo.setConnectionToUse(springCon); depo.setTransactionAware(transactionAware); depos.add(depo); } return depos; }