Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Embedded Apache Derby

I know a Similar question has been asked and answered before. But i've tried the solutions from the previous answers and they are not working out. I've tried out as much as i could and hope someone can shed some lights over the problem i'm facing.

Configuring embedded Derby in Spring Boot app

Spring-boot error using Apache Derby as embedded database

Spring Boot non-embedded Derby database?

And a lot more from the web. But seriously, i don't see a solution yet for the probelm i'm facing.

Here is my code,

application.properties

spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
spring.datasource.url=jdbc:derby:memory:mydb;create=true
spring.datasource.driver-class-name=org.apache.derby.jdbc.EmbeddedDriver

ApacheDerbyExample.java

package com.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.boot.CommandLineRunner;

@ComponentScan("com.app")
@SpringBootApplication
public class ApacheDerbyExample implements CommandLineRunner {

    public static void main(String[] args) {
        System.out.println("STARTING THE APPLICATION");
        SpringApplication.run(ApacheDerbyExample.class, args);
        System.out.println("APPLICATION FINISHED");
    }

    @Override
    public void run(String... args) {
        System.out.println("EXECUTING : command line runner");
        //org.apache.derby.jdbc.ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
        //ds.setDatabaseName("mydb");
    }
}

And from my pom.xml, org.apache.derby derby 10.15.2.0 runtime org.apache.derby derbyclient 10.15.2.0 runtime

https://github.com/manikandaraj/practice-code/tree/master/java/spring-boot-apache-derby-embedded

I'm building the project using,

mvn clean package

And when i run,

java -jar -Dspring.config.location=config/spring-conf/application.properties target/derby-101-0.0.1-SNAPSHOT.jar

But i'm getting the following error,

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-05-31 02:15:43.402 ERROR 8733 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Dbcp2.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.commons.dbcp2.BasicDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: org.apache.derby.jdbc.EmbeddedDriver
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]

I'm just trying to use the Apache Derby Embedded Database in my Application and with the dependencies defined in Maven, I don't know why i'm still getting the error.

I want to be able to use the Embedded Pooled Data Source(Derby) and run some query on CSV content I get from REST APIs.

like image 536
Manikandaraj Srinivasan Avatar asked Oct 18 '25 14:10

Manikandaraj Srinivasan


2 Answers

I think ,Looks like you are missing the jdbc driver in your classpath.

like image 63
Cemalettin Altınsoy Avatar answered Oct 21 '25 04:10

Cemalettin Altınsoy


Mani, Please add below annotation in your Application class

@EnableAutoConfiguration(exclude = { //
    DataSourceAutoConfiguration.class, //
    DataSourceTransactionManagerAutoConfiguration.class, //
    HibernateJpaAutoConfiguration.class }

)

Also remove the below version from your pom.xml..

<version>2.7.0</version>
like image 25
user2858852 Avatar answered Oct 21 '25 04:10

user2858852



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!