Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Spring-Boot Autowired JdbcTemplate with Derby

I have cloned a copy of gs-relational-data-access-complete onto my workspace, downloaded a standalone copy of Apache Derby and launched it "./bin/startNetworkServer".

I want to point my Spring Boot project to use an external instance of Apache Derby instead of the default embedded H2 database that Spring Boot uses. To save time I would like to use the Spring Boot @Autowired annotation.

When I launch a fresh copy of Derby there is no issue:

./bin/startNetworkServer

Sat Nov 21 11:11:06 GMT 2015 : Security manager installed using the Basic server security policy.

Sat Nov 21 11:11:06 GMT 2015 : Apache Derby Network Server - 10.12.1.1 - (1704137) started and ready to accept connections on port 1527

The problem arises when I launch the Spring Boot application:

2015-11-21 11:55:07.312 ERROR 11361 --- [ main] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.

java.sql.SQLException: Driver:org.apache.derby.jdbc.EmbeddedDriver@db9d03fc returned null for URL:jdbc:derby://10.12.1.1:1527/example1;create=true;user=test1;password=pass1

at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:326) ~[tomcat-jdbc-8.0.28.jar:na]

Any help on how to resolve this issue while still taking advantage of the Spring Boot @Autowired annotation would be greatly appreciated.

My workspace is as follows:

pom.xml
src/main/java/hello
----Application.java
----Customer.java
src/main/resources
----application.properties

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-relational-data-access</artifactId>
<version>0.1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.RELEASE</version>
</parent>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

application.properties:

spring.datasource.url=jdbc:derby://10.12.1.1:1527/example1;create=true;user=test1;password=pass1
spring.datasource.username=test1
spring.datasource.password=pass1

Application.java:

package hello;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@SpringBootApplication
public class Application implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String args[]) {
        SpringApplication.run(Application.class, args);
    }
    
    @Autowired
    JdbcTemplate jdbcTemplate;
like image 406
Hugh Pearse Avatar asked Sep 27 '22 03:09

Hugh Pearse


1 Answers

This is a mismatch:

Driver:org.apache.derby.jdbc.EmbeddedDriver@db9d03fc returned null 
for URL:jdbc:derby://10.12.1.1:1527/example1;create=true;user=test1;password=pass1

You're specifying to use the Derby embedded driver, but you've given Derby a client-server URL for the database.

Derby is detecting that mismatch and refusing your connection attempt.

If you want to use a client-server URL, you need to use the Derby ClientDriver class, and you need to have derbyclient.jar in your CLASSPATH.

If you want to use the embedded driver, you need to use an embedded driver URL, such as

jdbc:derby:example1;create=true;user=test1;password=pass1
like image 69
Bryan Pendleton Avatar answered Sep 30 '22 08:09

Bryan Pendleton