use of javax.sql.DataSource in project spring-boot by spring-projects.
the class NarayanaXADataSourceWrapperTests method wrap.
@Test
public void wrap() {
DataSource wrapped = this.wrapper.wrapDataSource(this.dataSource);
assertThat(wrapped).isInstanceOf(NarayanaDataSourceBean.class);
verify(this.recoveryManager, times(1)).registerXAResourceRecoveryHelper(any(DataSourceXAResourceRecoveryHelper.class));
verify(this.properties, times(1)).getRecoveryDbUser();
verify(this.properties, times(1)).getRecoveryDbPass();
}
use of javax.sql.DataSource in project spring-framework by spring-projects.
the class LocalDataSourceJobStore method initialize.
@Override
public void initialize(ClassLoadHelper loadHelper, SchedulerSignaler signaler) throws SchedulerConfigException {
// Absolutely needs thread-bound DataSource to initialize.
this.dataSource = SchedulerFactoryBean.getConfigTimeDataSource();
if (this.dataSource == null) {
throw new SchedulerConfigException("No local DataSource found for configuration - " + "'dataSource' property must be set on SchedulerFactoryBean");
}
// Configure transactional connection settings for Quartz.
setDataSource(TX_DATA_SOURCE_PREFIX + getInstanceName());
setDontSetAutoCommitFalse(true);
// Register transactional ConnectionProvider for Quartz.
DBConnectionManager.getInstance().addConnectionProvider(TX_DATA_SOURCE_PREFIX + getInstanceName(), new ConnectionProvider() {
@Override
public Connection getConnection() throws SQLException {
// Return a transactional Connection, if any.
return DataSourceUtils.doGetConnection(dataSource);
}
@Override
public void shutdown() {
// Do nothing - a Spring-managed DataSource has its own lifecycle.
}
/* Quartz 2.2 initialize method */
public void initialize() {
// Do nothing - a Spring-managed DataSource has its own lifecycle.
}
});
// Non-transactional DataSource is optional: fall back to default
// DataSource if not explicitly specified.
DataSource nonTxDataSource = SchedulerFactoryBean.getConfigTimeNonTransactionalDataSource();
final DataSource nonTxDataSourceToUse = (nonTxDataSource != null ? nonTxDataSource : this.dataSource);
// Configure non-transactional connection settings for Quartz.
setNonManagedTXDataSource(NON_TX_DATA_SOURCE_PREFIX + getInstanceName());
// Register non-transactional ConnectionProvider for Quartz.
DBConnectionManager.getInstance().addConnectionProvider(NON_TX_DATA_SOURCE_PREFIX + getInstanceName(), new ConnectionProvider() {
@Override
public Connection getConnection() throws SQLException {
// Always return a non-transactional Connection.
return nonTxDataSourceToUse.getConnection();
}
@Override
public void shutdown() {
// Do nothing - a Spring-managed DataSource has its own lifecycle.
}
/* Quartz 2.2 initialize method */
public void initialize() {
// Do nothing - a Spring-managed DataSource has its own lifecycle.
}
});
// No, if HSQL is the platform, we really don't want to use locks...
try {
String productName = JdbcUtils.extractDatabaseMetaData(this.dataSource, "getDatabaseProductName").toString();
productName = JdbcUtils.commonDatabaseName(productName);
if (productName != null && productName.toLowerCase().contains("hsql")) {
setUseDBLocks(false);
setLockHandler(new SimpleSemaphore());
}
} catch (MetaDataAccessException ex) {
logWarnIfNonZero(1, "Could not detect database type. Assuming locks can be taken.");
}
super.initialize(loadHelper, signaler);
}
use of javax.sql.DataSource in project spring-framework by spring-projects.
the class SimpleNamingContextTests method testNamingContextBuilder.
@Test
public void testNamingContextBuilder() throws NamingException {
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
InitialContextFactory factory = builder.createInitialContextFactory(null);
DataSource ds = new StubDataSource();
builder.bind("java:comp/env/jdbc/myds", ds);
Object obj = new Object();
builder.bind("myobject", obj);
Context context1 = factory.getInitialContext(null);
assertTrue("Correct DataSource registered", context1.lookup("java:comp/env/jdbc/myds") == ds);
assertTrue("Correct Object registered", context1.lookup("myobject") == obj);
Hashtable<String, String> env2 = new Hashtable<>();
env2.put("key1", "value1");
Context context2 = factory.getInitialContext(env2);
assertTrue("Correct DataSource registered", context2.lookup("java:comp/env/jdbc/myds") == ds);
assertTrue("Correct Object registered", context2.lookup("myobject") == obj);
assertTrue("Correct environment", context2.getEnvironment() != env2);
assertTrue("Correct key1", "value1".equals(context2.getEnvironment().get("key1")));
Integer i = new Integer(0);
context1.rebind("myinteger", i);
String s = "";
context2.bind("mystring", s);
Context context3 = (Context) context2.lookup("");
context3.rename("java:comp/env/jdbc/myds", "jdbc/myds");
context3.unbind("myobject");
assertTrue("Correct environment", context3.getEnvironment() != context2.getEnvironment());
context3.addToEnvironment("key2", "value2");
assertTrue("key2 added", "value2".equals(context3.getEnvironment().get("key2")));
context3.removeFromEnvironment("key1");
assertTrue("key1 removed", context3.getEnvironment().get("key1") == null);
assertTrue("Correct DataSource registered", context1.lookup("jdbc/myds") == ds);
try {
context1.lookup("myobject");
fail("Should have thrown NameNotFoundException");
} catch (NameNotFoundException ex) {
// expected
}
assertTrue("Correct Integer registered", context1.lookup("myinteger") == i);
assertTrue("Correct String registered", context1.lookup("mystring") == s);
assertTrue("Correct DataSource registered", context2.lookup("jdbc/myds") == ds);
try {
context2.lookup("myobject");
fail("Should have thrown NameNotFoundException");
} catch (NameNotFoundException ex) {
// expected
}
assertTrue("Correct Integer registered", context2.lookup("myinteger") == i);
assertTrue("Correct String registered", context2.lookup("mystring") == s);
assertTrue("Correct DataSource registered", context3.lookup("jdbc/myds") == ds);
try {
context3.lookup("myobject");
fail("Should have thrown NameNotFoundException");
} catch (NameNotFoundException ex) {
// expected
}
assertTrue("Correct Integer registered", context3.lookup("myinteger") == i);
assertTrue("Correct String registered", context3.lookup("mystring") == s);
Map<String, Binding> bindingMap = new HashMap<>();
NamingEnumeration<?> bindingEnum = context3.listBindings("");
while (bindingEnum.hasMoreElements()) {
Binding binding = (Binding) bindingEnum.nextElement();
bindingMap.put(binding.getName(), binding);
}
assertTrue("Correct jdbc subcontext", bindingMap.get("jdbc").getObject() instanceof Context);
assertTrue("Correct jdbc subcontext", SimpleNamingContext.class.getName().equals(bindingMap.get("jdbc").getClassName()));
Context jdbcContext = (Context) context3.lookup("jdbc");
jdbcContext.bind("mydsX", ds);
Map<String, Binding> subBindingMap = new HashMap<>();
NamingEnumeration<?> subBindingEnum = jdbcContext.listBindings("");
while (subBindingEnum.hasMoreElements()) {
Binding binding = (Binding) subBindingEnum.nextElement();
subBindingMap.put(binding.getName(), binding);
}
assertTrue("Correct DataSource registered", ds.equals(subBindingMap.get("myds").getObject()));
assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subBindingMap.get("myds").getClassName()));
assertTrue("Correct DataSource registered", ds.equals(subBindingMap.get("mydsX").getObject()));
assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subBindingMap.get("mydsX").getClassName()));
assertTrue("Correct Integer registered", i.equals(bindingMap.get("myinteger").getObject()));
assertTrue("Correct Integer registered", Integer.class.getName().equals(bindingMap.get("myinteger").getClassName()));
assertTrue("Correct String registered", s.equals(bindingMap.get("mystring").getObject()));
assertTrue("Correct String registered", String.class.getName().equals(bindingMap.get("mystring").getClassName()));
context1.createSubcontext("jdbc").bind("sub/subds", ds);
Map<String, String> pairMap = new HashMap<>();
NamingEnumeration<?> pairEnum = context2.list("jdbc");
while (pairEnum.hasMore()) {
NameClassPair pair = (NameClassPair) pairEnum.next();
pairMap.put(pair.getName(), pair.getClassName());
}
assertTrue("Correct sub subcontext", SimpleNamingContext.class.getName().equals(pairMap.get("sub")));
Context subContext = (Context) context2.lookup("jdbc/sub");
Map<String, String> subPairMap = new HashMap<>();
NamingEnumeration<?> subPairEnum = subContext.list("");
while (subPairEnum.hasMoreElements()) {
NameClassPair pair = (NameClassPair) subPairEnum.next();
subPairMap.put(pair.getName(), pair.getClassName());
}
assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subPairMap.get("subds")));
assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("myds")));
assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("mydsX")));
pairMap.clear();
pairEnum = context1.list("jdbc/");
while (pairEnum.hasMore()) {
NameClassPair pair = (NameClassPair) pairEnum.next();
pairMap.put(pair.getName(), pair.getClassName());
}
assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("myds")));
assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("mydsX")));
}
use of javax.sql.DataSource in project spring-framework by spring-projects.
the class InitializeDatabaseIntegrationTests method testScriptNameWithPattern.
@Test
public void testScriptNameWithPattern() throws Exception {
context = new ClassPathXmlApplicationContext("org/springframework/jdbc/config/jdbc-initialize-pattern-config.xml");
DataSource dataSource = context.getBean("dataSource", DataSource.class);
assertCorrectSetup(dataSource);
JdbcTemplate t = new JdbcTemplate(dataSource);
assertEquals("Dave", t.queryForObject("select name from T_TEST", String.class));
}
use of javax.sql.DataSource in project spring-framework by spring-projects.
the class InitializeDatabaseIntegrationTests method testCacheInitialization.
@Test
public void testCacheInitialization() throws Exception {
context = new ClassPathXmlApplicationContext("org/springframework/jdbc/config/jdbc-initialize-cache-config.xml");
assertCorrectSetup(context.getBean("dataSource", DataSource.class));
CacheData cache = context.getBean(CacheData.class);
assertEquals(1, cache.getCachedData().size());
}
Aggregations