本文整理汇总了Java中org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy类的典型用法代码示例。如果您正苦于以下问题:Java LazyConnectionDataSourceProxy类的具体用法?Java LazyConnectionDataSourceProxy怎么用?Java LazyConnectionDataSourceProxy使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LazyConnectionDataSourceProxy类属于org.springframework.jdbc.datasource包,在下文中一共展示了LazyConnectionDataSourceProxy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ReplicationRoutingDataSource
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; //导入依赖的package包/类
public ReplicationRoutingDataSource(DataSource master, List<DataSource> slaves) {
notNull(master, "master datasource mast be set.");
this.master = new LazyConnectionDataSourceProxy(master);
if (null != slaves) {
this.slaves = slaves;
}
setDefaultTargetDataSource(this.master);
Map<Object, Object> dataSourceMap = new HashMap<Object, Object>();
dataSourceMap.put("master", this.master);
if (null != slaves && !slaves.isEmpty()) {
for (int i = 0; i < slaves.size(); i++) {
dataSourceMap.put("slave_" + i, new LazyConnectionDataSourceProxy(slaves.get(i)));
}
}
setTargetDataSources(dataSourceMap);
}
示例2: shutdownLazyConnectionDataSourceProxys
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; //导入依赖的package包/类
/**
* 關閉
*
* @return
*/
protected LazyConnectionDataSourceProxy[] shutdownLazyConnectionDataSourceProxys() throws Exception {
try {
if (this.lazyConnectionDataSourceProxys != null) {
for (int i = 0; i < this.lazyConnectionDataSourceProxys.length; i++) {
LazyConnectionDataSourceProxy oldInstance = this.lazyConnectionDataSourceProxys[i];
// oldInstance.close();
this.lazyConnectionDataSourceProxys[i] = null;
}
//
this.lazyConnectionDataSourceProxys = null;
}
} catch (Exception e) {
LOGGER.error(new StringBuilder("Exception encountered during shutdownLazyConnectionDataSourceProxys()")
.toString(), e);
throw e;
}
return this.lazyConnectionDataSourceProxys;
}
示例3: afterPropertiesSet
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; //导入依赖的package包/类
@Override
public void afterPropertiesSet() throws Exception {
if (this.shardsDataSources == null) {
throw new IllegalArgumentException("Property 'shardsDataSources' is required");
}
this.resolvedShardsDataSources = new HashMap<Object, DataSource>(this.shardsDataSources.size());
for (Map.Entry<Object, Object> entry : this.shardsDataSources.entrySet()) {
DataSource dataSource = resolveSpecifiedDataSource(entry.getValue());
LazyConnectionDataSourceProxy lazyDataSourceProxy = new LazyConnectionDataSourceProxy();
lazyDataSourceProxy.setTargetDataSource(dataSource);
this.resolvedShardsDataSources.put(entry.getKey(), lazyDataSourceProxy);
}
if (this.defaultDataSource == null) {
throw new IllegalArgumentException("Property 'defaultDataSource' is required");
}
if(this.defaultDataSource != null){
resolvedDefaultDataSource = this.resolveSpecifiedDataSource(defaultDataSource);
}
}
示例4: getConnection_NoReadReplicaAvailableNoTransactionActive_returnsDefaultDataSource
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; //导入依赖的package包/类
@Test
public void getConnection_NoReadReplicaAvailableNoTransactionActive_returnsDefaultDataSource() throws Exception {
//Arrange
DataSource defaultDataSource = mock(DataSource.class);
Connection connection = mock(Connection.class);
when(defaultDataSource.getConnection()).thenReturn(connection);
ReadOnlyRoutingDataSource readOnlyRoutingDataSource = new ReadOnlyRoutingDataSource();
readOnlyRoutingDataSource.setTargetDataSources(Collections.emptyMap());
readOnlyRoutingDataSource.setDefaultTargetDataSource(defaultDataSource);
readOnlyRoutingDataSource.afterPropertiesSet();
LazyConnectionDataSourceProxy dataSource = new LazyConnectionDataSourceProxy(readOnlyRoutingDataSource);
//Act
Connection connectionReturned = dataSource.getConnection();
//Assert
assertSame(connection, ((ConnectionProxy) connectionReturned).getTargetConnection());
}
示例5: dataSource
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; //导入依赖的package包/类
/**
* Primary data source.
*
* @return the primary data source
*/
@Bean
@Primary
public DataSource dataSource() throws SQLException {
DataSource dataSourceTarget = new HikariDataSource(getHikariConfig());
return new LazyConnectionDataSourceProxy(dataSourceTarget);
}
示例6: createLazyConnectionDataSourceProxys
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; //导入依赖的package包/类
/**
* 建構
*
* @return
*/
protected LazyConnectionDataSourceProxy[] createLazyConnectionDataSourceProxys(DataSource[] dataSources)
throws Exception {
AssertHelper.notNull(dataSources, "The DataSources must not be null");
//
LazyConnectionDataSourceProxy[] result = null;
try {
int size = dataSources.length;
result = new LazyConnectionDataSourceProxy[size];
for (int i = 0; i < size; i++) {
LazyConnectionDataSourceProxy lazyConnectionDataSourceProxy = new LazyConnectionDataSourceProxy(
dataSources[i]);
result[i] = lazyConnectionDataSourceProxy;
}
} catch (Exception e) {
LOGGER.error(
new StringBuilder("Exception encountered during createLazyConnectionDataSourceProxys()").toString(),
e);
try {
result = (LazyConnectionDataSourceProxy[]) shutdownLazyConnectionDataSourceProxys();
} catch (Exception sie) {
throw sie;
}
throw e;
}
return result;
}
示例7: restartLazyConnectionDataSourceProxys
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; //导入依赖的package包/类
/**
* 重啟
*
* @return
*/
protected LazyConnectionDataSourceProxy[] restartLazyConnectionDataSourceProxys() throws Exception {
try {
if (this.lazyConnectionDataSourceProxys != null) {
this.lazyConnectionDataSourceProxys = shutdownLazyConnectionDataSourceProxys();
this.lazyConnectionDataSourceProxys = createLazyConnectionDataSourceProxys(this.dataSources);
}
} catch (Exception e) {
LOGGER.error(new StringBuilder("Exception encountered during restartLazyConnectionDataSourceProxys()")
.toString(), e);
throw e;
}
return this.lazyConnectionDataSourceProxys;
}
示例8: setUpBeforeClass
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; //导入依赖的package包/类
@BeforeClass
public static void setUpBeforeClass() throws Exception {
applicationContext = new ClassPathXmlApplicationContext(new String[] { //
"applicationContext-init.xml", //
"org/openyu/commons/spring/jdbc/datasource/testContext-lazy-group.xml",//
});
lazyConnectionDataSourceProxys = applicationContext.getBean("lazyConnectionDataSourceProxyGroupFactoryBean",
LazyConnectionDataSourceProxy[].class);
}
开发者ID:mixaceh,项目名称:openyu-commons,代码行数:11,代码来源:LazyConnectionDataSourceProxyGroupFactoryBeanTest.java
示例9: lazyConnectionDataSourceProxys
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; //导入依赖的package包/类
@Test
@BenchmarkOptions(benchmarkRounds = 1, warmupRounds = 0, concurrency = 1)
public void lazyConnectionDataSourceProxys() throws Exception {
System.out.println(lazyConnectionDataSourceProxys);
assertNotNull(lazyConnectionDataSourceProxys);
//
for (LazyConnectionDataSourceProxy lazyConnectionDataSourceProxy : lazyConnectionDataSourceProxys) {
System.out.println(lazyConnectionDataSourceProxy.getTargetDataSource().getConnection());
}
}
开发者ID:mixaceh,项目名称:openyu-commons,代码行数:11,代码来源:LazyConnectionDataSourceProxyGroupFactoryBeanTest.java
示例10: testClobStringTypeWithSynchronizedConnection
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; //导入依赖的package包/类
@Test
public void testClobStringTypeWithSynchronizedConnection() throws Exception {
DataSource dsTarget = new DriverManagerDataSource();
DataSource ds = new LazyConnectionDataSourceProxy(dsTarget);
given(lobHandler.getClobAsString(rs, 1)).willReturn("content");
ClobStringTypeHandler type = new ClobStringTypeHandler(lobHandler);
assertEquals("content", type.valueOf("content"));
assertEquals("content", type.getResult(rs, "column"));
assertEquals("content", type.getResult(rs, 1));
TransactionSynchronizationManager.initSynchronization();
try {
DataSourceUtils.getConnection(ds);
type.setParameter(ps, 1, "content", null);
List synchs = TransactionSynchronizationManager.getSynchronizations();
assertEquals(2, synchs.size());
assertTrue(synchs.get(0).getClass().getName().endsWith("LobCreatorSynchronization"));
((TransactionSynchronization) synchs.get(0)).beforeCompletion();
((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
((TransactionSynchronization) synchs.get(1)).beforeCompletion();
((TransactionSynchronization) synchs.get(1)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
}
finally {
TransactionSynchronizationManager.clearSynchronization();
}
verify(lobCreator).setClobAsString(ps, 1, "content");
}
示例11: createInstance
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; //导入依赖的package包/类
/**
* Constructs a {@link org.springframework.cloud.aws.jdbc.datasource.ReadOnlyRoutingDataSource} data source that contains the
* regular data source as a default, and all read-replicas as additional data source. The {@link
* org.springframework.cloud.aws.jdbc.datasource.ReadOnlyRoutingDataSource} is additionally wrapped with a {@link
* org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy}, because the read-only flag is only available
* after the transactional context has been established. This is only the case if the physical connection is
* requested after the transaction start and not while starting a transaction.
*
* @return a ReadOnlyRoutingDataSource that is wrapped with a LazyConnectionDataSourceProxy
* @throws Exception
* if the underlying data source setup throws any exception
*/
@Override
protected DataSource createInstance() throws Exception {
DBInstance dbInstance = getDbInstance(getDbInstanceIdentifier());
//If there is no read replica available, delegate to super class
if (dbInstance.getReadReplicaDBInstanceIdentifiers().isEmpty()) {
return super.createInstance();
}
HashMap<Object, Object> replicaMap = new HashMap<>(
dbInstance.getReadReplicaDBInstanceIdentifiers().size());
for (String replicaName : dbInstance.getReadReplicaDBInstanceIdentifiers()) {
replicaMap.put(replicaName, createDataSourceInstance(replicaName));
}
//Create the data source
ReadOnlyRoutingDataSource dataSource = new ReadOnlyRoutingDataSource();
dataSource.setTargetDataSources(replicaMap);
dataSource.setDefaultTargetDataSource(createDataSourceInstance(getDbInstanceIdentifier()));
//Initialize the class
dataSource.afterPropertiesSet();
return new LazyConnectionDataSourceProxy(dataSource);
}
开发者ID:spring-cloud,项目名称:spring-cloud-aws,代码行数:39,代码来源:AmazonRdsReadReplicaAwareDataSourceFactoryBean.java
示例12: destroyInstance
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; //导入依赖的package包/类
@Override
protected void destroyInstance(DataSource instance) throws Exception {
if (instance instanceof LazyConnectionDataSourceProxy) {
DataSource targetDataSource = ((LazyConnectionDataSourceProxy) instance).getTargetDataSource();
if (targetDataSource instanceof ReadOnlyRoutingDataSource) {
List<Object> dataSources = ((ReadOnlyRoutingDataSource) targetDataSource).getDataSources();
for (Object candidate : dataSources) {
if (candidate instanceof DataSource) {
super.destroyInstance(instance);
}
}
}
}
}
开发者ID:spring-cloud,项目名称:spring-cloud-aws,代码行数:15,代码来源:AmazonRdsReadReplicaAwareDataSourceFactoryBean.java
示例13: getConnection_NoReadReplicaAvailableReadOnlyTransactionActive_returnsDefaultDataSource
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; //导入依赖的package包/类
@Test
public void getConnection_NoReadReplicaAvailableReadOnlyTransactionActive_returnsDefaultDataSource() throws Exception {
//Arrange
DataSource defaultDataSource = mock(DataSource.class);
Connection connection = mock(Connection.class);
when(defaultDataSource.getConnection()).thenReturn(connection);
ReadOnlyRoutingDataSource readOnlyRoutingDataSource = new ReadOnlyRoutingDataSource();
readOnlyRoutingDataSource.setTargetDataSources(Collections.emptyMap());
readOnlyRoutingDataSource.setDefaultTargetDataSource(defaultDataSource);
readOnlyRoutingDataSource.afterPropertiesSet();
LazyConnectionDataSourceProxy dataSource = new LazyConnectionDataSourceProxy(readOnlyRoutingDataSource);
DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
transactionDefinition.setReadOnly(true);
TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(dataSource), transactionDefinition);
//Act
Connection connectionReturned = transactionTemplate.execute(status -> {
try {
return ((ConnectionProxy) dataSource.getConnection()).getTargetConnection();
} catch (SQLException e) {
fail(e.getMessage());
}
return null;
});
//Assert
assertSame(connection, connectionReturned);
}
示例14: getConnection_ReadReplicaAvailableReadOnlyTransactionActive_returnsReadReplicaDataSource
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; //导入依赖的package包/类
@Test
public void getConnection_ReadReplicaAvailableReadOnlyTransactionActive_returnsReadReplicaDataSource() throws Exception {
//Arrange
DataSource defaultDataSource = mock(DataSource.class);
Connection connection = mock(Connection.class);
DataSource readOnlyDataSource = mock(DataSource.class);
Connection readOnlyConnection = mock(Connection.class);
when(readOnlyDataSource.getConnection()).thenReturn(readOnlyConnection);
when(defaultDataSource.getConnection()).thenReturn(connection);
ReadOnlyRoutingDataSource readOnlyRoutingDataSource = new ReadOnlyRoutingDataSource();
readOnlyRoutingDataSource.setTargetDataSources(Collections.singletonMap("read1", readOnlyDataSource));
readOnlyRoutingDataSource.setDefaultTargetDataSource(defaultDataSource);
readOnlyRoutingDataSource.afterPropertiesSet();
LazyConnectionDataSourceProxy dataSource = new LazyConnectionDataSourceProxy(readOnlyRoutingDataSource);
DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
transactionDefinition.setReadOnly(true);
TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(dataSource), transactionDefinition);
//Act
Connection connectionReturned = transactionTemplate.execute(status -> {
try {
return ((ConnectionProxy) dataSource.getConnection()).getTargetConnection();
} catch (SQLException e) {
fail(e.getMessage());
}
return null;
});
//Assert
assertSame(readOnlyConnection, connectionReturned);
}
示例15: getConnection_ReadReplicaAvailableWriteTransactionActive_returnsDefaultDataSource
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; //导入依赖的package包/类
@Test
public void getConnection_ReadReplicaAvailableWriteTransactionActive_returnsDefaultDataSource() throws Exception {
//Arrange
DataSource defaultDataSource = mock(DataSource.class);
Connection connection = mock(Connection.class);
DataSource readOnlyDataSource = mock(DataSource.class);
Connection readOnlyConnection = mock(Connection.class);
when(readOnlyDataSource.getConnection()).thenReturn(readOnlyConnection);
when(defaultDataSource.getConnection()).thenReturn(connection);
ReadOnlyRoutingDataSource readOnlyRoutingDataSource = new ReadOnlyRoutingDataSource();
readOnlyRoutingDataSource.setTargetDataSources(Collections.singletonMap("read1", readOnlyDataSource));
readOnlyRoutingDataSource.setDefaultTargetDataSource(defaultDataSource);
readOnlyRoutingDataSource.afterPropertiesSet();
LazyConnectionDataSourceProxy dataSource = new LazyConnectionDataSourceProxy(readOnlyRoutingDataSource);
DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
transactionDefinition.setReadOnly(false);
TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(dataSource), transactionDefinition);
//Act
Connection connectionReturned = transactionTemplate.execute(status -> {
try {
return ((ConnectionProxy) dataSource.getConnection()).getTargetConnection();
} catch (SQLException e) {
fail(e.getMessage());
}
return null;
});
//Assert
assertSame(connection, connectionReturned);
}