简体   繁体   English

使用Derby配置Spring-Boot Autowired JdbcTemplate

[英]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". 我已将gs-relational-data-access-complete的副本克隆到我的工作区,下载了Apache Derby的独立副本并将其启动为“./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. 我想指出我的Spring Boot项目使用Apache Derby的外部实例,而不是Spring Boot使用的默认嵌入式H2数据库。 To save time I would like to use the Spring Boot @Autowired annotation. 为了节省时间,我想使用Spring Boot @Autowired注释。

When I launch a fresh copy of Derby there is no issue: 当我发布Derby的新副本时,没有问题:

./bin/startNetworkServer ./bin/startNetworkServer

Sat Nov 21 11:11:06 GMT 2015 : Security manager installed using the Basic server security policy. 11月21日星期六11:11:06 GMT 2015:使用基本服务器安全策略安装安全管理器。

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 11月21日星期六11:11:06 GMT 2015:Apache Derby网络服务器 - 10.12.1.1 - (1704137)启动并准备接受端口1527上的连接

The problem arises when I launch the Spring Boot application: 当我启动Spring Boot应用程序时出现问题:

2015-11-21 11:55:07.312 ERROR 11361 --- [ main] oatomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool. 2015-11-21 11:55:07.312 ERROR 11361 --- [main] oatomcat.jdbc.pool.ConnectionPool:无法创建池的初始连接。

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 java.sql.SQLException:驱动程序:org.apache.derby.jdbc.EmbeddedDriver@db9d03fc为URL返回null: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] 在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. 如何在仍然利用Spring Boot @Autowired注释的同时解决此问题的任何帮助将不胜感激。

My workspace is as follows: 我的工作区如下:

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

pom.xml : 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: 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: 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;

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 嵌入式驱动程序,但您已为Derby提供了数据库的客户端 - 服务器 URL。

Derby is detecting that mismatch and refusing your connection attempt. Derby检测到不匹配并拒绝您的连接尝试。

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. 如果要使用客户端 - 服务器 URL,则需要使用Derby ClientDriver类,并且需要在CLASSPATH中使用derbyclient.jar。

If you want to use the embedded driver, you need to use an embedded driver URL, such as 如果要使用嵌入式驱动程序,则需要使用嵌入式驱动程序URL,例如

jdbc:derby:example1;create=true;user=test1;password=pass1

Change 更改

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
</dependency>

by 通过

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derbyclient</artifactId>
    <version>10.13.1.1</version>
</dependency>

and in the configuration: 并在配置中:

spring.datasource.url=jdbc:derby://10.12.1.1:1527/example1;create=true
spring.datasource.username=test1
spring.datasource.password=pass1
spring.datasource.driver-class-name=org.apache.derby.jdbc.ClientDriver

The problem is that derby dependency is for embedded database, you need the client dependency. 问题是derby依赖是针对嵌入式数据库的,需要客户端依赖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM