io.bootique.jdbc.LazyDataSource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bootique-jdbc Show documentation
Show all versions of bootique-jdbc Show documentation
Provides JDBC integration mechanism for Bootique without implementing a specific DataSource.
Requires an extra implementor module in runtime, such as 'bootique-jdbc-tomcat", etc.
The newest version!
/*
* Licensed to ObjectStyle LLC under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ObjectStyle LLC licenses
* this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.bootique.jdbc;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.*;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.logging.Logger;
/**
* A DataSource wrapper delegating to another DataSource that is created lazily on first access.
*
* @since 3.0
*/
public class LazyDataSource implements DataSource, AutoCloseable {
private final Supplier supplier;
private volatile DataSource delegate;
public LazyDataSource(Supplier supplier) {
this.supplier = supplier;
}
protected DataSource getDelegate() {
if (delegate == null) {
synchronized (this) {
if (delegate == null) {
delegate = Objects.requireNonNull(supplier.get());
}
}
}
return delegate;
}
@Override
public void close() throws Exception {
DataSource local = this.delegate;
if (local instanceof AutoCloseable) {
((AutoCloseable) local).close();
}
}
@Override
public Connection getConnection() throws SQLException {
return getDelegate().getConnection();
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return getDelegate().getConnection(username, password);
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return getDelegate().getLogWriter();
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
getDelegate().setLogWriter(out);
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
getDelegate().setLoginTimeout(seconds);
}
@Override
public int getLoginTimeout() throws SQLException {
return getDelegate().getLoginTimeout();
}
@Override
public ConnectionBuilder createConnectionBuilder() throws SQLException {
return getDelegate().createConnectionBuilder();
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return getDelegate().getParentLogger();
}
@Override
public ShardingKeyBuilder createShardingKeyBuilder() throws SQLException {
return getDelegate().createShardingKeyBuilder();
}
@Override
public T unwrap(Class iface) throws SQLException {
return iface.isInstance(this) ? (T) this : getDelegate().unwrap(iface);
}
@Override
public boolean isWrapperFor(Class> iface) throws SQLException {
return iface.isInstance(this) ? true : getDelegate().isWrapperFor(iface);
}
}