Skip to content

Commit

Permalink
add ConnectionPool.from(url), ConnectionPool.from(url, user, password…
Browse files Browse the repository at this point in the history
…), ConnectionPool.from(url, properties), NonBlockingConnectionPool.Builder.user(user) and .password(password) #34
  • Loading branch information
davidmoten committed Nov 8, 2018
1 parent 97105f3 commit 1d2aedf
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package org.davidmoten.rx.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.annotation.Nonnull;

import org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException;

import com.github.davidmoten.guavamini.Preconditions;

/**
Expand All @@ -18,17 +23,26 @@ public interface ConnectionProvider {
*
* @return a new Connection to a database
*/
@Nonnull
@Nonnull
Connection get();

/**
* Closes the connection provider and releases its resources. For example, a
* connection pool may need formal closure to release its connections
* because connection.close() is actually just releasing a connection back
* to the pool for reuse. This method should be idempotent.
* connection pool may need formal closure to release its connections because
* connection.close() is actually just releasing a connection back to the pool
* for reuse. This method should be idempotent.
*/
void close();

/**
* Warning: Don't pass one of these as a ConnectionProvider to a pool because
* once the pool closes the connection the pool cannot create a new one (the
* same closed connection is returned). Instead use a different ConnectionProvider
* factory method.
*
* @param connection connection for singleton provider
* @return singleton connection provider (don't use with connection pools!)
*/
static ConnectionProvider from(@Nonnull Connection connection) {
Preconditions.checkNotNull(connection, "connection cannot be null");
return new ConnectionProvider() {
Expand All @@ -45,4 +59,65 @@ public void close() {
};
}

static ConnectionProvider from(@Nonnull String url) {
Preconditions.checkNotNull(url, "url cannot be null");
return new ConnectionProvider() {

@Override
public Connection get() {
try {
return DriverManager.getConnection(url);
} catch (SQLException e) {
throw new SQLRuntimeException(e);
}
}

@Override
public void close() {
// do nothing as closure will be handle by pool
}
};
}

static ConnectionProvider from(@Nonnull String url, String username, String password) {
Preconditions.checkNotNull(url, "url cannot be null");
return new ConnectionProvider() {

@Override
public Connection get() {
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
throw new SQLRuntimeException(e);
}
}

@Override
public void close() {
// do nothing as closure will be handle by pool
}
};
}

static ConnectionProvider from(@Nonnull String url, Properties properties) {
Preconditions.checkNotNull(url, "url cannot be null");
Preconditions.checkNotNull(properties, "properties cannot be null");
return new ConnectionProvider() {

@Override
public Connection get() {
try {
return DriverManager.getConnection(url, properties);
} catch (SQLException e) {
throw new SQLRuntimeException(e);
}
}

@Override
public void close() {
// do nothing as closure will be handle by pool
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ public Builder<T> url(String url) {
this.url = url;
return this;
}

public Builder<T> user(String user) {
properties.put("user", user);
return this;
}

public Builder<T> password(String password) {
properties.put("password", password);
return this;
}



/**
* Sets the JDBC properties that will be passed to
Expand Down

0 comments on commit 1d2aedf

Please sign in to comment.