-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
work on #4 blocking connection pools
- Loading branch information
1 parent
686d2e6
commit 5247244
Showing
6 changed files
with
145 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/org/davidmoten/rx/jdbc/pool/ConnectionProviderBlockingPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package org.davidmoten.rx.jdbc.pool; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
import org.davidmoten.rx.jdbc.ConnectionProvider; | ||
import org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException; | ||
import org.davidmoten.rx.pool.Member; | ||
import org.davidmoten.rx.pool.MemberWithValue; | ||
import org.davidmoten.rx.pool.Pool; | ||
|
||
import io.reactivex.Single; | ||
import io.reactivex.plugins.RxJavaPlugins; | ||
|
||
public final class ConnectionProviderBlockingPool implements Pool<Connection> { | ||
|
||
private final ConnectionProvider connectionProvider; | ||
|
||
public ConnectionProviderBlockingPool(ConnectionProvider connectionProvider) { | ||
this.connectionProvider = connectionProvider; | ||
} | ||
|
||
@Override | ||
public Single<Member<Connection>> member() { | ||
return Single.fromCallable(() -> { | ||
return new MemberWithValue<Connection>() { | ||
|
||
Connection connection; | ||
volatile boolean shutdown = false; | ||
|
||
@Override | ||
public void close() throws Exception { | ||
connectionProvider.close(); | ||
} | ||
|
||
@Override | ||
public MemberWithValue<Connection> checkout() { | ||
// blocking | ||
connection = connectionProvider.get(); | ||
return this; | ||
} | ||
|
||
@Override | ||
public void checkin() { | ||
try { | ||
connection.close(); | ||
} catch (SQLException e) { | ||
throw new SQLRuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
shutdown = true; | ||
try { | ||
connection.close(); | ||
} catch (SQLException e) { | ||
RxJavaPlugins.onError(e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isShutdown() { | ||
return shutdown; | ||
} | ||
|
||
@Override | ||
public Connection value() { | ||
return connection; | ||
} | ||
|
||
}; | ||
}); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
connectionProvider.close(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters