本文整理匯總了Java中org.springframework.jdbc.core.ConnectionCallback類的典型用法代碼示例。如果您正苦於以下問題:Java ConnectionCallback類的具體用法?Java ConnectionCallback怎麽用?Java ConnectionCallback使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ConnectionCallback類屬於org.springframework.jdbc.core包,在下文中一共展示了ConnectionCallback類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: AbstractDbDialect
import org.springframework.jdbc.core.ConnectionCallback; //導入依賴的package包/類
public AbstractDbDialect(final JdbcTemplate jdbcTemplate, LobHandler lobHandler){
this.jdbcTemplate = jdbcTemplate;
this.lobHandler = lobHandler;
// 初始化transction
this.transactionTemplate = new TransactionTemplate();
transactionTemplate.setTransactionManager(new DataSourceTransactionManager(jdbcTemplate.getDataSource()));
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
// 初始化一些數據
jdbcTemplate.execute(new ConnectionCallback() {
public Object doInConnection(Connection c) throws SQLException, DataAccessException {
DatabaseMetaData meta = c.getMetaData();
databaseName = meta.getDatabaseProductName();
databaseMajorVersion = meta.getDatabaseMajorVersion();
databaseMinorVersion = meta.getDatabaseMinorVersion();
return null;
}
});
initTables(jdbcTemplate);
}
示例2: findAll
import org.springframework.jdbc.core.ConnectionCallback; //導入依賴的package包/類
@Override
public Page<T> findAll(final Predicate predicate, final Pageable pageable, final Sort... sorts) {
return jdbcTemplate.execute(new ConnectionCallback<Page<T>>(){
public Page<T> doInConnection(Connection con) throws SQLException, DataAccessException {
SQLQuery query = new SQLQuery(con, dialect).from(metamodel);
applyWhere(query, predicate);
applySorting(query, sorts);
applyPagination(query, pageable);
List<T> content = query.list(metamodel);
SQLQuery countQuery = new SQLQuery(con, dialect).from(metamodel);
applyWhere(countQuery, predicate);
long total = countQuery.count();
return new Pagination<T>(content, pageable, total);
}
});
}
示例3: findSqlServerPKIndex
import org.springframework.jdbc.core.ConnectionCallback; //導入依賴的package包/類
/**
* 查找sqlserver的主鍵索引
*
* @param dbInofId
* @param tableName
* @return 返回主鍵索引的值
* @throws Exception
*/
public String findSqlServerPKIndex(String dbInofId, final String tableName) throws Exception {
DataSource ds = getDataSourceByDbInfoId(dbInofId);
JdbcTemplate jdbcTemplate = SpringJdbcUtils.getJdbcTemplate(ds);
String s = jdbcTemplate.execute(new ConnectionCallback<String>() {
public String doInConnection(Connection con) throws SQLException, DataAccessException {
String pkName = null;
if (con.getMetaData().getURL().toLowerCase().contains("sqlserver")) {
CallableStatement call = con.prepareCall("{call sp_pkeys(?)}");
call.setString(1, tableName);
ResultSet rs = call.executeQuery();
while (rs.next()) {
pkName = rs.getString("PK_NAME");
}
}
return pkName;
}
});
return s;
}
示例4: judgeDbType
import org.springframework.jdbc.core.ConnectionCallback; //導入依賴的package包/類
/**
* 根據DataSource判斷一下數據庫類型
*
* @param dataSource
* @return
*/
public static DbType judgeDbType(DataSource dataSource) {
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
return (DbType) jdbcTemplate.execute(new ConnectionCallback() {
public Object doInConnection(Connection c) throws SQLException, DataAccessException {
DatabaseMetaData meta = c.getMetaData();
String databaseName = meta.getDatabaseProductName();
String version = meta.getDatabaseProductVersion();
if (StringUtils.startsWithIgnoreCase(databaseName, "oracle")) {
return DbType.ORACLE;
} else if (StringUtils.startsWithIgnoreCase(databaseName, "mysql")) {
if (StringUtils.contains(version, "-TDDL-")) {
return DbType.DRDS;
} else {
return DbType.MYSQL;
}
} else {
throw new YuGongException("unknow database type " + databaseName);
}
}
});
}
示例5: judgeDbType
import org.springframework.jdbc.core.ConnectionCallback; //導入依賴的package包/類
/**
* 根據DataSource判斷一下數據庫類型
*
* @param dataSource
* @return
*/
public static DbType judgeDbType(DataSource dataSource) {
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
return (DbType) jdbcTemplate.execute(new ConnectionCallback() {
public Object doInConnection(Connection c) throws SQLException, DataAccessException {
DatabaseMetaData meta = c.getMetaData();
String databaseName = meta.getDatabaseProductName();
String version = meta.getDatabaseProductVersion();
if (StringUtils.startsWithIgnoreCase(databaseName, "oracle")) {
return DbType.ORACLE;
} else if (StringUtils.startsWithIgnoreCase(databaseName, "mysql")) {
if (StringUtils.contains(version, "-TDDL-")) {
return DbType.DRDS;
} else {
return DbType.MYSQL;
}
} else {
throw new YuGongException("unknow database type " + databaseName);
}
}
});
}
示例6: multipleDataSources
import org.springframework.jdbc.core.ConnectionCallback; //導入依賴的package包/類
@Test
public void multipleDataSources() {
load(MultipleDataSourcesConfig.class);
PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class);
Collection<Metric<?>> metrics = bean.metrics();
assertMetrics(metrics, "datasource.tomcat.active", "datasource.tomcat.usage",
"datasource.commonsDbcp.active", "datasource.commonsDbcp.usage");
// Hikari won't work unless a first connection has been retrieved
JdbcTemplate jdbcTemplate = new JdbcTemplate(
this.context.getBean("hikariDS", DataSource.class));
jdbcTemplate.execute(new ConnectionCallback<Void>() {
@Override
public Void doInConnection(Connection connection)
throws SQLException, DataAccessException {
return null;
}
});
Collection<Metric<?>> anotherMetrics = bean.metrics();
assertMetrics(anotherMetrics, "datasource.tomcat.active",
"datasource.tomcat.usage", "datasource.hikariDS.active",
"datasource.hikariDS.usage", "datasource.commonsDbcp.active",
"datasource.commonsDbcp.usage");
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:26,代碼來源:PublicMetricsAutoConfigurationTests.java
示例7: getPoolSizeOneConnection
import org.springframework.jdbc.core.ConnectionCallback; //導入依賴的package包/類
@Test
public void getPoolSizeOneConnection() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(
getDataSourceMetadata().getDataSource());
jdbcTemplate.execute(new ConnectionCallback<Void>() {
@Override
public Void doInConnection(Connection connection)
throws SQLException, DataAccessException {
assertThat(getDataSourceMetadata().getActive())
.isEqualTo(Integer.valueOf(1));
assertThat(getDataSourceMetadata().getUsage())
.isEqualTo(Float.valueOf(0.5F));
return null;
}
});
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:17,代碼來源:AbstractDataSourcePoolMetadataTests.java
示例8: detectPlatform
import org.springframework.jdbc.core.ConnectionCallback; //導入依賴的package包/類
/**
* Gets the platform information from the {@link DataSource}.
*
* @param dataSource the {@link DataSource} to consult.
* @return the platform information from the {@link DataSource}.
*/
public static DatabasePlatformInfo detectPlatform(DataSource dataSource) {
if (dataSource == null) {
throw new IllegalArgumentException("DataSource must not be null.");
}
DatabasePlatformInfo platformInfo = platformCache.get(dataSource);
if (platformInfo == null) {
JdbcTemplate template = new JdbcTemplate(dataSource);
platformCache.put(dataSource, template.execute(new ConnectionCallback<DatabasePlatformInfo>() {
@Override
public DatabasePlatformInfo doInConnection(
Connection connection) throws SQLException, DataAccessException {
DatabaseMetaData metadata = connection.getMetaData();
String vendorName = metadata.getDatabaseProductName();
int version = metadata.getDatabaseMajorVersion();
return new DatabasePlatformInfo(vendorName, version);
}
}));
if (platformInfo == null) {
platformInfo = platformCache.get(dataSource);
}
}
return platformInfo;
}
示例9: verifyTestEnvironment
import org.springframework.jdbc.core.ConnectionCallback; //導入依賴的package包/類
public static void verifyTestEnvironment(DataSource dataSource) {
if (dataSource == null) {
Assert.fail("Could not locate the data source.");
}
JdbcTemplate template = new JdbcTemplate(dataSource);
template.execute(new ConnectionCallback() {
public Object doInConnection(Connection connection) throws SQLException {
ResultSet resultSet = connection.getMetaData().getTables(null, null, TEST_TABLE_NAME, null);
if (!resultSet.next()) {
LOG.error("No table named '"+TEST_TABLE_NAME+"' was found in the configured database. " +
"You are attempting to run tests against a non-test database!!!");
LOG.error("The test environment will not start up properly!!!");
Assert.fail("No table named '"+TEST_TABLE_NAME+"' was found in the configured database. " +
"You are attempting to run tests against a non-test database!!!");
}
return null;
}
});
}
示例10: countTableResults
import org.springframework.jdbc.core.ConnectionCallback; //導入依賴的package包/類
protected int countTableResults(String valueToVerify) {
final String valueToCheck = valueToVerify;
final DataSource dataSource = TestHarnessServiceLocator.getDataSource();
return (Integer) new JdbcTemplate(dataSource).execute(new ConnectionCallback() {
public Object doInConnection(final Connection connection) throws SQLException {
Statement statement = null;
try {
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
final ResultSet resultSet = statement.executeQuery("Select * from " + TEST_TABLE_NAME + " where COL = '" + valueToCheck + "'");
assertNotNull("ResultSet should not be null",resultSet);
int count = 0;
while (resultSet.next()) {
count++;
}
return count;
} finally {
if (statement != null) {
statement.close();
}
}
}
});
}
示例11: getTableNames
import org.springframework.jdbc.core.ConnectionCallback; //導入依賴的package包/類
/**
* @return a list of the table names, retrieved via the connection metadata object. The excludeTableNames
* property can be used to ignore certain table names.
*/
protected List<String> getTableNames() {
return new JdbcTemplate(dataSource).execute(new ConnectionCallback<List<String>>() {
@Override
public List<String> doInConnection(Connection con) throws SQLException, DataAccessException {
ResultSet rs = con.getMetaData().getTables(null, null, "%", new String[]{"TABLE"});
List<String> list = new ArrayList<>();
while (rs.next()) {
String name = rs.getString("TABLE_NAME");
if (excludeTableNames == null || !excludeTableNames.contains(name)) {
list.add(name);
}
}
return list;
}
});
}
示例12: execute
import org.springframework.jdbc.core.ConnectionCallback; //導入依賴的package包/類
@Override @SuppressWarnings("unchecked")
public <R, X> R execute(final TemplateCallback<R,X> callback) {
Class<?> type = callback.getTemplateType();
if(ClassUtils.isAssignable(Connection.class, type)){
return template.execute(new ConnectionCallback<R>() {
public R doInConnection(Connection conn) throws SQLException, DataAccessException {
return callback.doInTemplate((X)conn);
}
});
}else if(ClassUtils.isAssignable(JdbcTemplate.class, type)){
return callback.doInTemplate((X)template);
}else{
throw new RuntimeException("Template Type is wrong.");
}
}
示例13: execute
import org.springframework.jdbc.core.ConnectionCallback; //導入依賴的package包/類
@Override
public Long execute() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
return jdbcTemplate.execute(new ConnectionCallback<Long>() {
@Override
public Long doInConnection(Connection connection) throws SQLException, DataAccessException {
final SQLDeleteClause deleteClause = new SQLDeleteClause(connection, configuration, pathBase);
for (BooleanExpression expression : QueryExampleHelper.getExpressions(pathBase, example)) {
deleteClause.where(expression);
}
for (SQLBindings sqlBindings : deleteClause.getSQL()) {
LOGGER.debug("SQL: {} \nparams: {}", sqlBindings.getSQL(), sqlBindings.getBindings());
}
return deleteClause.execute();
}
});
}
示例14: runScripts
import org.springframework.jdbc.core.ConnectionCallback; //導入依賴的package包/類
public synchronized void runScripts(Stream<String> scripts) {
jdbcTemplate.execute((ConnectionCallback<Void>) connection -> {
IBatisPatchedScriptRunner scriptRunner = new IBatisPatchedScriptRunner(connection);
scriptRunner.setAutoCommit(false);
scriptRunner.setStopOnError(true);
scripts.forEach(script -> {
logger.debug("Running script {}.", script);
try (FileReader reader = new FileReader(script)) {
scriptRunner.runScript(reader);
} catch (IOException e) {
throw new RuntimeSqlException(e);
}
});
return null;
});
}
示例15: doHealthCheck
import org.springframework.jdbc.core.ConnectionCallback; //導入依賴的package包/類
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
log.debug("Initializing Database health indicator");
try {
String dataBaseProductName = jdbcTemplate.execute(new ConnectionCallback<String>() {
@Override
public String doInConnection(Connection connection)
throws SQLException, DataAccessException {
return connection.getMetaData().getDatabaseProductName();
}
});
query = detectQuery(dataBaseProductName);
builder.up();
} catch (Exception e) {
log.debug("Cannot connect to Database.", e);
builder.down(e);
}
}