Skip to content

Database <name> doesn't exist when trying to create database in 0.3.2 #826

@debychkov

Description

@debychkov

Hi, I'm updating our driver from 0.2.6 to 0.3.2 and found some difference in behavior.

We have few cases, when we need to create database from our code, and then write into it. Mostly in autotests, but not only. And that's when I ran into a problem - I cannot get the connection to create database, because there is no database. Minimal code example:

import java.sql.SQLException;

import org.springframework.jdbc.datasource.DataSourceUtils;

import ru.yandex.clickhouse.ClickHouseConnection;
import ru.yandex.clickhouse.ClickHouseDataSource;
import ru.yandex.clickhouse.settings.ClickHouseProperties;

public class CreateDatabaseExample {

    public static void main(String[] args) throws SQLException {
        String connectionUrl = "jdbc:clickhouse://localhost:8123/test_db";
        ClickHouseConnection clickHouseConnection = (ClickHouseConnection) DataSourceUtils.getConnection(
                new ClickHouseDataSource(connectionUrl, new ClickHouseProperties()));
        clickHouseConnection.createStatement().executeQuery("CREATE DATABASE IF NOT EXISTS test_db");
    }

}

This worked fine with 0.2.6, but now I get an exception:

Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is ru.yandex.clickhouse.except.ClickHouseException: ClickHouse exception, code: 81, host: localhost, port: 8123; Code: 81. DB::Exception: Database test_db doesn't exist. (UNKNOWN_DATABASE) (version 21.12.3.32 (official build))

	at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:82)
	at ru.yandex.metrika.clickhouse.CreateDatabaseExample.main(CreateDatabaseExample.java:19)
Caused by: ru.yandex.clickhouse.except.ClickHouseException: ClickHouse exception, code: 81, host: localhost, port: 8123; Code: 81. DB::Exception: Database test_db doesn't exist. (UNKNOWN_DATABASE) (version 21.12.3.32 (official build))

	at ru.yandex.clickhouse.except.ClickHouseExceptionSpecifier.specify(ClickHouseExceptionSpecifier.java:60)
	at ru.yandex.clickhouse.except.ClickHouseExceptionSpecifier.specify(ClickHouseExceptionSpecifier.java:30)
	at ru.yandex.clickhouse.ClickHouseStatementImpl.checkForErrorAndThrow(ClickHouseStatementImpl.java:1083)
	at ru.yandex.clickhouse.ClickHouseStatementImpl.getInputStream(ClickHouseStatementImpl.java:770)
	at ru.yandex.clickhouse.ClickHouseStatementImpl.getLastInputStream(ClickHouseStatementImpl.java:693)
	at ru.yandex.clickhouse.ClickHouseStatementImpl.executeQuery(ClickHouseStatementImpl.java:341)
	at ru.yandex.clickhouse.ClickHouseStatementImpl.executeQuery(ClickHouseStatementImpl.java:326)
	at ru.yandex.clickhouse.ClickHouseStatementImpl.executeQuery(ClickHouseStatementImpl.java:320)
	at ru.yandex.clickhouse.ClickHouseStatementImpl.executeQuery(ClickHouseStatementImpl.java:314)
	at ru.yandex.clickhouse.ClickHouseConnectionImpl.initConnection(ClickHouseConnectionImpl.java:95)
	at ru.yandex.clickhouse.ClickHouseConnectionImpl.<init>(ClickHouseConnectionImpl.java:79)
	at ru.yandex.clickhouse.ClickHouseDriver.connect(ClickHouseDriver.java:65)
	at ru.yandex.clickhouse.ClickHouseDataSource.getConnection(ClickHouseDataSource.java:44)
	at ru.yandex.clickhouse.ClickHouseDataSource.getConnection(ClickHouseDataSource.java:15)
	at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158)
	at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116)
	at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)
	... 1 more
Caused by: java.lang.Throwable: Code: 81. DB::Exception: Database test_db doesn't exist. (UNKNOWN_DATABASE) (version 21.12.3.32 (official build))

	at ru.yandex.clickhouse.except.ClickHouseExceptionSpecifier.specify(ClickHouseExceptionSpecifier.java:55)
	... 17 more

The query for timezone and version is failing with the exception: https://github.com/ClickHouse/clickhouse-jdbc/blob/master/clickhouse-jdbc/src/main/java/ru/yandex/clickhouse/ClickHouseConnectionImpl.java#L95

Is it an expected behavior? Is there a workaround to this problem? Of course we can create connection without database, create db, then create another connection, but it would be nice to use only one connection at a time, if possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions