Search in sources :

Example 36 with DriverManagerDataSource

use of org.springframework.jdbc.datasource.DriverManagerDataSource in project spring-framework by spring-projects.

the class RdbmsOperationTests method parameterPropagation.

@Test
public void parameterPropagation() {
    SqlOperation operation = new SqlOperation() {
    };
    DataSource ds = new DriverManagerDataSource();
    operation.setDataSource(ds);
    operation.setFetchSize(10);
    operation.setMaxRows(20);
    JdbcTemplate jt = operation.getJdbcTemplate();
    assertThat(jt.getDataSource()).isEqualTo(ds);
    assertThat(jt.getFetchSize()).isEqualTo(10);
    assertThat(jt.getMaxRows()).isEqualTo(20);
}
Also used : DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataSource(javax.sql.DataSource) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) Test(org.junit.jupiter.api.Test)

Example 37 with DriverManagerDataSource

use of org.springframework.jdbc.datasource.DriverManagerDataSource in project spring-framework by spring-projects.

the class PersistenceXmlParsingTests method testExampleComplex.

@Test
public void testExampleComplex() throws Exception {
    DataSource ds = new DriverManagerDataSource();
    String resource = "/org/springframework/orm/jpa/persistence-complex.xml";
    MapDataSourceLookup dataSourceLookup = new MapDataSourceLookup();
    Map<String, DataSource> dataSources = new HashMap<>();
    dataSources.put("jdbc/MyPartDB", ds);
    dataSources.put("jdbc/MyDB", ds);
    dataSourceLookup.setDataSources(dataSources);
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), dataSourceLookup);
    PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
    assertThat(info.length).isEqualTo(2);
    PersistenceUnitInfo pu1 = info[0];
    assertThat(pu1.getPersistenceUnitName()).isEqualTo("pu1");
    assertThat(pu1.getPersistenceProviderClassName()).isEqualTo("com.acme.AcmePersistence");
    assertThat(pu1.getMappingFileNames().size()).isEqualTo(1);
    assertThat(pu1.getMappingFileNames().get(0)).isEqualTo("ormap2.xml");
    assertThat(pu1.getJarFileUrls().size()).isEqualTo(1);
    assertThat(pu1.getJarFileUrls().get(0)).isEqualTo(new ClassPathResource("order.jar").getURL());
    assertThat(pu1.excludeUnlistedClasses()).isFalse();
    assertThat(pu1.getTransactionType()).isSameAs(PersistenceUnitTransactionType.RESOURCE_LOCAL);
    Properties props = pu1.getProperties();
    assertThat(props.keySet().size()).isEqualTo(2);
    assertThat(props.getProperty("com.acme.persistence.sql-logging")).isEqualTo("on");
    assertThat(props.getProperty("foo")).isEqualTo("bar");
    assertThat(pu1.getNonJtaDataSource()).isNull();
    assertThat(pu1.getJtaDataSource()).isSameAs(ds);
    assertThat(pu1.excludeUnlistedClasses()).as("Exclude unlisted should default false in 1.0.").isFalse();
    PersistenceUnitInfo pu2 = info[1];
    assertThat(pu2.getTransactionType()).isSameAs(PersistenceUnitTransactionType.JTA);
    assertThat(pu2.getPersistenceProviderClassName()).isEqualTo("com.acme.AcmePersistence");
    assertThat(pu2.getMappingFileNames().size()).isEqualTo(1);
    assertThat(pu2.getMappingFileNames().get(0)).isEqualTo("order2.xml");
    // the following assertions fail only during coverage runs
    // assertEquals(1, pu2.getJarFileUrls().size());
    // assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), pu2.getJarFileUrls().get(0));
    assertThat(pu2.excludeUnlistedClasses()).isTrue();
    assertThat(pu2.getJtaDataSource()).isNull();
    assertThat(pu2.getNonJtaDataSource()).isEqualTo(ds);
    assertThat(pu2.excludeUnlistedClasses()).as("Exclude unlisted should be true when no value.").isTrue();
}
Also used : HashMap(java.util.HashMap) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) Properties(java.util.Properties) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) ClassPathResource(org.springframework.core.io.ClassPathResource) DataSource(javax.sql.DataSource) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) MapDataSourceLookup(org.springframework.jdbc.datasource.lookup.MapDataSourceLookup) Test(org.junit.jupiter.api.Test)

Example 38 with DriverManagerDataSource

use of org.springframework.jdbc.datasource.DriverManagerDataSource in project spring-framework by spring-projects.

the class PersistenceXmlParsingTests method testExample4.

@Test
public void testExample4() throws Exception {
    SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
    DataSource ds = new DriverManagerDataSource();
    builder.bind("java:comp/env/jdbc/MyDB", ds);
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
    String resource = "/org/springframework/orm/jpa/persistence-example4.xml";
    PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
    assertThat(info).isNotNull();
    assertThat(info.length).isEqualTo(1);
    assertThat(info[0].getPersistenceUnitName()).isEqualTo("OrderManagement4");
    assertThat(info[0].getMappingFileNames().size()).isEqualTo(1);
    assertThat(info[0].getMappingFileNames().get(0)).isEqualTo("order-mappings.xml");
    assertThat(info[0].getManagedClassNames().size()).isEqualTo(3);
    assertThat(info[0].getManagedClassNames().get(0)).isEqualTo("com.acme.Order");
    assertThat(info[0].getManagedClassNames().get(1)).isEqualTo("com.acme.Customer");
    assertThat(info[0].getManagedClassNames().get(2)).isEqualTo("com.acme.Item");
    assertThat(info[0].excludeUnlistedClasses()).as("Exclude unlisted should be true when no value.").isTrue();
    assertThat(info[0].getTransactionType()).isSameAs(PersistenceUnitTransactionType.RESOURCE_LOCAL);
    assertThat(info[0].getProperties().keySet().size()).isEqualTo(0);
    builder.clear();
}
Also used : SimpleNamingContextBuilder(org.springframework.context.testfixture.jndi.SimpleNamingContextBuilder) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) DataSource(javax.sql.DataSource) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) Test(org.junit.jupiter.api.Test)

Example 39 with DriverManagerDataSource

use of org.springframework.jdbc.datasource.DriverManagerDataSource in project spring-data-jdbc by spring-projects.

the class Db2DataSourceConfiguration method createDataSource.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.jdbc.testing.DataSourceConfiguration#createDataSource()
	 */
@Override
protected DataSource createDataSource() {
    if (DB_2_CONTAINER == null) {
        LOG.info("DB2 starting...");
        Db2Container container = new Db2Container().withReuse(true);
        container.start();
        LOG.info("DB2 started");
        DB_2_CONTAINER = container;
    }
    return new DriverManagerDataSource(DB_2_CONTAINER.getJdbcUrl(), DB_2_CONTAINER.getUsername(), DB_2_CONTAINER.getPassword());
}
Also used : DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) Db2Container(org.testcontainers.containers.Db2Container)

Example 40 with DriverManagerDataSource

use of org.springframework.jdbc.datasource.DriverManagerDataSource in project spring-data-jdbc by spring-projects.

the class OracleDataSourceConfiguration method initDb.

private void initDb() {
    final DriverManagerDataSource dataSource = new DriverManagerDataSource(ORACLE_CONTAINER.getJdbcUrl(), "SYSTEM", ORACLE_CONTAINER.getPassword());
    final JdbcTemplate jdbc = new JdbcTemplate(dataSource);
    jdbc.execute("GRANT ALL PRIVILEGES TO " + ORACLE_CONTAINER.getUsername());
}
Also used : DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Aggregations

DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)66 Bean (org.springframework.context.annotation.Bean)29 LocalContainerEntityManagerFactoryBean (org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)20 Test (org.junit.jupiter.api.Test)9 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)8 DataSource (javax.sql.DataSource)7 SQLException (java.sql.SQLException)4 Properties (java.util.Properties)4 PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 Connection (java.sql.Connection)2 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 InitialContext (javax.naming.InitialContext)2 XADataSource (javax.sql.XADataSource)2 Before (org.junit.Before)2 ApplicationContextHolder (org.mifos.application.servicefacade.ApplicationContextHolder)2 ServletRegistrationBean (org.springframework.boot.web.servlet.ServletRegistrationBean)2