Search in sources :

Example 61 with DataSource

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();
}
Also used : DataSource(javax.sql.DataSource) XADataSource(javax.sql.XADataSource) Test(org.junit.Test)

Example 62 with DataSource

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);
}
Also used : MetaDataAccessException(org.springframework.jdbc.support.MetaDataAccessException) SimpleSemaphore(org.quartz.impl.jdbcjobstore.SimpleSemaphore) SQLException(java.sql.SQLException) Connection(java.sql.Connection) SchedulerConfigException(org.quartz.SchedulerConfigException) ConnectionProvider(org.quartz.utils.ConnectionProvider) DataSource(javax.sql.DataSource)

Example 63 with DataSource

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")));
}
Also used : SimpleNamingContextBuilder(org.springframework.tests.mock.jndi.SimpleNamingContextBuilder) InitialContext(javax.naming.InitialContext) SimpleNamingContext(org.springframework.tests.mock.jndi.SimpleNamingContext) Context(javax.naming.Context) Binding(javax.naming.Binding) NameNotFoundException(javax.naming.NameNotFoundException) HashMap(java.util.HashMap) Hashtable(java.util.Hashtable) InitialContextFactory(javax.naming.spi.InitialContextFactory) DataSource(javax.sql.DataSource) NameClassPair(javax.naming.NameClassPair) Test(org.junit.Test)

Example 64 with DataSource

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));
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 65 with DataSource

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());
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Aggregations

DataSource (javax.sql.DataSource)546 Connection (java.sql.Connection)200 Test (org.junit.Test)192 SQLException (java.sql.SQLException)118 Context (javax.naming.Context)70 ResultSet (java.sql.ResultSet)59 Statement (java.sql.Statement)59 NamingException (javax.naming.NamingException)57 InitialContext (javax.naming.InitialContext)55 EJBException (javax.ejb.EJBException)40 HashMap (java.util.HashMap)38 PreparedStatement (java.sql.PreparedStatement)37 Properties (java.util.Properties)35 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)34 RemoteException (java.rmi.RemoteException)32 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)31 UserTransaction (javax.transaction.UserTransaction)30 IOException (java.io.IOException)29 ArrayList (java.util.ArrayList)26 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)21