11

I'm trying to run my program, I always get this exception:

Caused by: org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:137)
at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:117)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 45 more

I'm importing all dependencies via gradle:

buildscript {
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.7.RELEASE")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
baseName = 'flatify-backend-service'
version =  '0.1.0'
}

repositories {
mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.hibernate:hibernate-core:4.3.6.Final'
compile 'javax.servlet:javax.servlet-api:3.1.0'
compile 'org.javassist:javassist:3.15.0-GA'
compile 'mysql:mysql-connector-java:5.1.31'
compile 'commons-dbcp:commons-dbcp:1.4'
testCompile("junit:junit")
testCompile("org.springframework:spring-test")
}

task wrapper(type: Wrapper) {
gradleVersion = '2.5'
}

As you can see I'm adding the mysql-connector isn't that what's supposed to add the driver classes to my project? Am I missing something?

I only added to last exception, because all others are caused by this one. If you need any other details please let my know.

Thanks

My Config class:

@Configuration
@EnableTransactionManagement
public class PersistenceJPAConfig {

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(new String[] { "at.flatify.persistance.entity" });

    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(additionalProperties());

    return em;
}


@Bean(destroyMethod = "close")
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/flatify");
    dataSource.setUsername("user");
    dataSource.setPassword("password");

    return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf);

    return transactionManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
    return new PersistenceExceptionTranslationPostProcessor();
}

Properties additionalProperties() {
    Properties properties = new Properties();
    properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
    return properties;
}

}
Mathias Mahlknecht
  • 281
  • 2
  • 6
  • 16
  • Possible duplicate of [Spring Boot - Cannot determine embedded database driver class for database type NONE](http://stackoverflow.com/questions/24074749/spring-boot-cannot-determine-embedded-database-driver-class-for-database-type) – K.C. Jan 08 '16 at 14:56

3 Answers3

0

Spring boot is unable to determine which driver to use. You need to specify the following property somewhere: spring.datasource.driverClassName There are other properties which you will need to specify, check the documentation.

  • Yes, I have a Configuration class where I specified all these properties. – Mathias Mahlknecht Nov 14 '15 at 15:41
  • Can you paste the relevant properties (including any connection urls) This would help? Replacing any credentials with dummy ones of course. – Daniel Burrell Nov 14 '15 at 15:43
  • Looking at that stack trace, it looks like DataSourceAutoConfiguration is kicking in disregarding the setup you've got in your config. (since you're using spring boot) Have you tried excluding DataSourceAutoConfiguration.class ? Aside from that though, if you want to go the springboot route, it requires you to specifically provide spring.datasource.driverClassName in an application.properties file, it is not suficient to just set the driver class name in your config. – Daniel Burrell Nov 14 '15 at 15:49
  • I add "@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})" on top of my class, right? No changes, still the same error. Or do I have to put the exclude some where else? – Mathias Mahlknecht Nov 14 '15 at 15:54
  • I already add a application.properties file to my project root. Although I'm not sure why I need that, isn't it redundant? But anyway with the file or without, I always get the same exception. – Mathias Mahlknecht Nov 14 '15 at 15:58
0

I Faced the same Problem, I solved it by simply adding an eclipselink-javax.persistence-2.0 jar in the classpath of my project. you can download it from the link given below. or simply add the add the following dependency

<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.0.0</version>
</dependency>

Jar download Link

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0

You either need to disable (exclude) DataSourceAutoConfiguration or delete your class PersistenceJPAConfig configuration for the datasource and configure it using application.properties.

A good example in spring docs: https://spring.io/guides/gs/accessing-data-mysql/