本文整理汇总了Java中org.springframework.jdbc.datasource.DataSourceUtils.getConnection方法的典型用法代码示例。如果您正苦于以下问题:Java DataSourceUtils.getConnection方法的具体用法?Java DataSourceUtils.getConnection怎么用?Java DataSourceUtils.getConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.jdbc.datasource.DataSourceUtils
的用法示例。
在下文中一共展示了DataSourceUtils.getConnection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeExecuteFlag
import org.springframework.jdbc.datasource.DataSourceUtils; //导入方法依赖的package包/类
@Override
public void writeExecuteFlag(String appId, String busCode, String trxId, String pAppId, String pBusCode, String pTrxId, int status) {
Connection connection = DataSourceUtils.getConnection(selctor.selectDataSource(appId, busCode, trxId));
try {
PreparedStatement prepareStatement = connection.prepareStatement("INSERT INTO `executed_trans` (`app_id`, `bus_code`, `trx_id`,`p_app_id`, `p_bus_code`, `p_trx_id`,`status`) VALUES ( ?, ?, ?, ?, ?, ?, ?);");
prepareStatement.setString(1, appId);
prepareStatement.setString(2, busCode);
prepareStatement.setString(3, trxId);
prepareStatement.setString(4, pAppId);
prepareStatement.setString(5, pBusCode);
prepareStatement.setString(6, pTrxId);
prepareStatement.setInt(7, status);
int executeUpdate = prepareStatement.executeUpdate();
if(executeUpdate != 1){
throw new RuntimeException(String.format("insert count(%s) Error!%s %s %s %s %s %s %s", executeUpdate,appId,busCode,trxId,pAppId,pBusCode,pTrxId,status));
}
} catch (SQLException e) {
throw new RuntimeException("insert Sql failed,check whether the same transaction has been executed?",e);
}
}
示例2: findDefaultColumnType
import org.springframework.jdbc.datasource.DataSourceUtils; //导入方法依赖的package包/类
public List<String> findDefaultColumnType(String dbInfoId) throws Exception {
DataSource ds = this.getDataSourceByDbInfoId(dbInfoId);
Connection conn = null;
ResultSet resultSet = null;
try {
conn = DataSourceUtils.getConnection(ds);
DatabaseMetaData metaData = conn.getMetaData();
resultSet = metaData.getTypeInfo();
List<String> list = new ArrayList<String>();
while (resultSet.next()) {
String typeName = resultSet.getString("TYPE_NAME").toUpperCase();
list.add(typeName);
}
return list;
} finally {
JdbcUtils.closeResultSet(resultSet);
JdbcUtils.closeConnection(conn);
}
}
示例3: loadTablePrimaryKeys
import org.springframework.jdbc.datasource.DataSourceUtils; //导入方法依赖的package包/类
private List<String> loadTablePrimaryKeys(String tableName)throws Exception{
DataSource ds=this.getJdbcTemplate().getDataSource();
Connection con = DataSourceUtils.getConnection(ds);
List<String> primaryKeyList=new ArrayList<String>();
Statement stmt = null;
ResultSet rs=null;
try{
DatabaseMetaData metaData = con.getMetaData();
rs = metaData.getPrimaryKeys(null, null, tableName.toUpperCase());
while (rs.next()) {
primaryKeyList.add(rs.getString("COLUMN_NAME"));
}
}finally{
JdbcUtils.closeResultSet(rs);
JdbcUtils.closeStatement(stmt);
JdbcUtils.closeConnection(con);
}
return primaryKeyList;
}
示例4: getNextKey
import org.springframework.jdbc.datasource.DataSourceUtils; //导入方法依赖的package包/类
/**
* Executes the SQL as specified by {@link #getSequenceQuery()}.
*/
@Override
protected long getNextKey() throws DataAccessException {
Connection con = DataSourceUtils.getConnection(getDataSource());
Statement stmt = null;
ResultSet rs = null;
try {
stmt = con.createStatement();
DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
rs = stmt.executeQuery(getSequenceQuery());
if (rs.next()) {
return rs.getLong(1);
}
else {
throw new DataAccessResourceFailureException("Sequence query did not return a result");
}
}
catch (SQLException ex) {
throw new DataAccessResourceFailureException("Could not obtain sequence value", ex);
}
finally {
JdbcUtils.closeResultSet(rs);
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
示例5: getConnection
import org.springframework.jdbc.datasource.DataSourceUtils; //导入方法依赖的package包/类
@Override
public Connection getConnection() throws SQLException {
TransactionProxy proxy = getProxy();
if (proxy != null) {
return proxy.getConnection();
}
//根据当前激活的数据源 获取jdbc链接
DataSource dataSource = DataSourceHolder.currentDataSource().getNative();
String dsId = switcher().currentDataSourceId();
Connection connection = DataSourceUtils.getConnection(dataSource);
proxy = new TransactionProxy(dsId, connection, dataSource);
addProxy(proxy);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(
"DataSource (" + (dsId == null ? "default" : dsId) + ") JDBC Connection ["
+ connection
+ "] will"
+ (proxy.isConnectionTransactional ? " " : " not ")
+ "be managed by Spring");
}
return connection;
}
示例6: execute
import org.springframework.jdbc.datasource.DataSourceUtils; //导入方法依赖的package包/类
/**
* Execute the given {@link DatabasePopulator} against the given {@link DataSource}.
* @param populator the {@code DatabasePopulator} to execute
* @param dataSource the {@code DataSource} to execute against
* @throws DataAccessException if an error occurs, specifically a {@link ScriptException}
*/
public static void execute(DatabasePopulator populator, DataSource dataSource) throws DataAccessException {
Assert.notNull(populator, "DatabasePopulator must be provided");
Assert.notNull(dataSource, "DataSource must be provided");
try {
Connection connection = DataSourceUtils.getConnection(dataSource);
try {
populator.populate(connection);
}
finally {
if (connection != null) {
DataSourceUtils.releaseConnection(connection, dataSource);
}
}
}
catch (Exception ex) {
if (ex instanceof ScriptException) {
throw (ScriptException) ex;
}
throw new UncategorizedScriptException("Failed to execute database script", ex);
}
}
示例7: executeSql
import org.springframework.jdbc.datasource.DataSourceUtils; //导入方法依赖的package包/类
protected void executeSql(String path) {
logger.info("executeSql : " + path);
Resource resource = new ClassPathResource(path, getClass());
ResourceDatabasePopulator rdp = new ResourceDatabasePopulator();
rdp.addScript(resource);
rdp.setSqlScriptEncoding("UTF-8");
rdp.setIgnoreFailedDrops(true);
rdp.setContinueOnError(false);
try (Connection conn = DataSourceUtils.getConnection(dataSource)) {
rdp.populate(conn);
}
catch (Exception e) {
throw new IllegalStateException("executeSql failed, path=" + path, e);
}
}
示例8: checkConnection
import org.springframework.jdbc.datasource.DataSourceUtils; //导入方法依赖的package包/类
/**
*/
private void checkConnection() {
Connection conn = DataSourceUtils.getConnection(jdbc.getDataSource());
assertNotNull(conn);
try {
assertFalse(conn.isClosed());
assertEquals(!ses.isWithinTransaction(), conn.getAutoCommit());
}
catch (SQLException e) {
throw new RuntimeException(e);
}
verifySameInstance(conn);
}
示例9: queryFactory
import org.springframework.jdbc.datasource.DataSourceUtils; //导入方法依赖的package包/类
@Bean
public PostgresQueryFactory queryFactory() {
final DataSource dataSource = dataSource();
return new PostgresQueryFactory(querydslConfiguration(), new Provider<Connection>() {
@Override
public Connection get() {
Connection conn = DataSourceUtils.getConnection(dataSource);
if (!DataSourceUtils.isConnectionTransactional(conn, dataSource)) {
throw new RuntimeException("Connection is not transactional");
}
return conn;
}
});
}
示例10: execute
import org.springframework.jdbc.datasource.DataSourceUtils; //导入方法依赖的package包/类
/**
* Execute the given DatabasePopulator against the given DataSource.
* @param populator the DatabasePopulator to execute
* @param dataSource the DataSource to execute against
*/
public static void execute(DatabasePopulator populator, DataSource dataSource) {
Assert.notNull(populator, "DatabasePopulator must be provided");
Assert.notNull(dataSource, "DataSource must be provided");
try {
Connection connection = DataSourceUtils.getConnection(dataSource);
try {
populator.populate(connection);
}
finally {
if (connection != null) {
DataSourceUtils.releaseConnection(connection, dataSource);
}
}
}
catch (Exception ex) {
throw new DataAccessResourceFailureException("Failed to execute database script", ex);
}
}
示例11: getColumns
import org.springframework.jdbc.datasource.DataSourceUtils; //导入方法依赖的package包/类
public List<Column> getColumns(String tableName) throws SQLException {
List<Column> columns = new ArrayList<Column>();
Connection connection = DataSourceUtils.getConnection(dataSource);
DatabaseMetaData metaData = connection.getMetaData();
ResultSet columnsResultSet = metaData.getColumns(null, metaData.getUserName(), tableName, "%");
while (columnsResultSet.next()) {
Column column = new Column();
column.setColumnName(columnsResultSet.getString("COLUMN_NAME").toLowerCase());
column.setDataType(columnsResultSet.getInt("DATA_TYPE"));
column.setTypeName(columnsResultSet.getString("TYPE_NAME"));
column.setColumnSize(columnsResultSet.getInt("COLUMN_SIZE"));
column.setNullable(columnsResultSet.getInt("NULLABLE"));
column.setRemarks(columnsResultSet.getString("REMARKS"));
column.setColumnDef(columnsResultSet.getString("COLUMN_DEF"));
column.setOrdinalPosition(columnsResultSet.getInt("ORDINAL_POSITION"));
column.setIsNullable(columnsResultSet.getString("IS_NULLABLE"));
column.setIsAutoincrement(columnsResultSet.getString("IS_AUTOINCREMENT"));
columns.add(column);
}
DataSourceUtils.releaseConnection(connection, dataSource);
return columns;
}
示例12: delete
import org.springframework.jdbc.datasource.DataSourceUtils; //导入方法依赖的package包/类
@Override
public String delete(final String accountid) throws ProvisioningException {
LOG.debug("Delete request received");
Connection conn = null;
try {
conn = DataSourceUtils.getConnection(dataSource);
try (PreparedStatement statement = conn.prepareStatement("DELETE FROM user WHERE userId=?")) {
statement.setString(1, accountid);
String query = "DELETE FROM user WHERE userId='" + accountid + "';";
LOG.debug("Execute query: " + query);
statement.executeUpdate();
}
return accountid;
} catch (SQLException e) {
throw new ProvisioningException("Delete operation failed", e);
} finally {
DataSourceUtils.releaseConnection(conn, dataSource);
}
}
示例13: populateDatabase
import org.springframework.jdbc.datasource.DataSourceUtils; //导入方法依赖的package包/类
@Bean
@Lazy(false)
public ResourceDatabasePopulator populateDatabase() throws SQLException {
final 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());
}
}
return populator;
}
示例14: getQueryRunner
import org.springframework.jdbc.datasource.DataSourceUtils; //导入方法依赖的package包/类
/**
* 添加支持jdbc datasource Transaction 支持的QueryRunner处理方式
*/
@Override
protected QueryRunner getQueryRunner() {
return new QueryRunner(dataSource){
/* (non-Javadoc)
* @see org.apache.commons.dbutils.AbstractQueryRunner#prepareConnection()
*/
@Override
protected Connection prepareConnection() throws SQLException {
return DataSourceUtils.getConnection(this.getDataSource());
}
/* (non-Javadoc)
* @see org.apache.commons.dbutils.AbstractQueryRunner#close(java.sql.Connection)
*/
@Override
protected void close(Connection conn) throws SQLException {
DataSourceUtils.releaseConnection(conn, getDataSource());
}
};
}
示例15: getRateTableColumnNames
import org.springframework.jdbc.datasource.DataSourceUtils; //导入方法依赖的package包/类
/**
* Returns a list of column names read from the rate table in the database.
* @return column names
*/
public List<String> getRateTableColumnNames() {
DataSource dataSource = jdbcTemplate.getDataSource();
Connection connection = DataSourceUtils.getConnection(dataSource);
List<String> columns = Collections.emptyList();
try {
columns = JDBCUtils.getAllColumnNames(connection, rateCard.getTableName());
} catch (SQLException e) {
throw new SessionInternalError("Could not read columns from rate card table.", e,
new String[] { "RateCardWS,rates,rate.card.cannot.read.rating.table" });
} finally {
DataSourceUtils.releaseConnection(connection, dataSource);
}
return columns;
}