Search in sources :

Example 61 with DriverManagerDataSource

use of org.springframework.jdbc.datasource.DriverManagerDataSource in project tutorials by eugenp.

the class PersistenceJPAConfig method dataSource.

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
    dataSource.setUrl(env.getProperty("jdbc.url"));
    dataSource.setUsername(env.getProperty("jdbc.user"));
    dataSource.setPassword(env.getProperty("jdbc.pass"));
    return dataSource;
}
Also used : DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Example 62 with DriverManagerDataSource

use of org.springframework.jdbc.datasource.DriverManagerDataSource in project evosuite by EvoSuite.

the class EvoEntityManagerFactory method createEMFWithSpring.

private EntityManagerFactory createEMFWithSpring() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    // DriverManagerDataSource uses the context classloader for some reason...
    ClassLoader cl1 = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(EvoEntityManagerFactory.class.getClassLoader());
    dataSource.setDriverClassName(org.hsqldb.jdbcDriver.class.getName());
    dataSource.setUrl("jdbc:hsqldb:mem:.");
    dataSource.setUsername("sa");
    dataSource.setPassword("");
    Thread.currentThread().setContextClassLoader(cl1);
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource);
    // search everything on classpath
    em.setPackagesToScan("");
    em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    try {
        /*
                The code in this class works fine on Mac, but somehow it crashes on Debian (Lux cluster).
                So, the following is just a workaround, although not fully understood while on Debian
                it was behaving differently
             */
        Field f = LocalContainerEntityManagerFactoryBean.class.getDeclaredField("internalPersistenceUnitManager");
        f.setAccessible(true);
        DefaultPersistenceUnitManager m = (DefaultPersistenceUnitManager) f.get(em);
        m.setDefaultPersistenceUnitRootLocation(null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Properties properties = new Properties();
    properties.setProperty("hibernate.show_sql", "true");
    properties.setProperty("hibernate.dialect", HSQLDialect.class.getName());
    properties.setProperty("hibernate.connection.shutdown", "true");
    properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
    properties.setProperty("hibernate.classloading.use_current_tccl_as_parent", "false");
    em.setJpaProperties(properties);
    em.afterPropertiesSet();
    return em.getObject();
}
Also used : HSQLDialect(org.hibernate.dialect.HSQLDialect) Field(java.lang.reflect.Field) DefaultPersistenceUnitManager(org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) HibernateJpaVendorAdapter(org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter) Properties(java.util.Properties) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Example 63 with DriverManagerDataSource

use of org.springframework.jdbc.datasource.DriverManagerDataSource in project simple-java by angryziber.

the class HibernatePhotoSpotRepositoryIntegrationTest method setUp.

@Before
public void setUp() throws Exception {
    dataSource = new DriverManagerDataSource("jdbc:h2:mem:hibernate;DB_CLOSE_DELAY=-1", "sa", "sa");
    System.setProperty("hibernate.dialect", H2Dialect.class.getName());
    System.setProperty("hibernate.hbm2ddl.auto", "create-drop");
    AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();
    sessionFactory.setDataSource(dataSource);
    sessionFactory.setAnnotatedClasses(new Class[] { PhotoSpot.class });
    sessionFactory.afterPropertiesSet();
    repo.hibernate = new HibernateTemplate(sessionFactory.getObject());
}
Also used : H2Dialect(org.hibernate.dialect.H2Dialect) AnnotationSessionFactoryBean(org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) Before(org.junit.Before)

Example 64 with DriverManagerDataSource

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

the class RdbmsOperationTests method declareParameterAfterCompile.

@Test
public void declareParameterAfterCompile() {
    operation.setDataSource(new DriverManagerDataSource());
    operation.setSql("select * from mytable");
    operation.compile();
    assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() -> operation.declareParameter(new SqlParameter(Types.INTEGER)));
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) Test(org.junit.jupiter.api.Test)

Example 65 with DriverManagerDataSource

use of org.springframework.jdbc.datasource.DriverManagerDataSource in project pentaho-platform by pentaho.

the class PooledDatasourceHelperTest method testSuccessfulDriverInitInConvertGeneric.

@Test
public void testSuccessfulDriverInitInConvertGeneric() throws DBDatasourceServiceException {
    when(databaseType.getShortName()).thenReturn("GENERIC");
    when(connection.getAttributes()).thenReturn(ImmutableMap.of(GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS, nativeDriverName));
    when(connection.getUsername()).thenReturn("suzy");
    when(connection.getPassword()).thenReturn("password");
    when(((IDriverLocator) driverLocatorDialect).initialize(nativeDriverName)).thenReturn(true);
    DriverManagerDataSource dataSource = (DriverManagerDataSource) PooledDatasourceHelper.convert(connection, () -> dialectService);
    verify(((IDriverLocator) driverLocatorDialect), times(1)).initialize(nativeDriverName);
    assertThat(dataSource.getUrl(), is(jdbcUrl));
    assertThat(dataSource.getUsername(), is("suzy"));
    assertThat(dataSource.getPassword(), is("password"));
}
Also used : DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) IDriverLocator(org.pentaho.database.IDriverLocator) Test(org.junit.Test)

Aggregations

DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)67 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