Skip to content

Commit

Permalink
fix: revert env variable 'typewriter.connection.singleton'
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Aug 28, 2024
1 parent ff73fa1 commit 40e6a53
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
16 changes: 11 additions & 5 deletions src/main/java/typewriter/rdb/ConnectionPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@
import java.util.concurrent.TimeUnit;

import kiss.I;
import kiss.WiseFunction;
import kiss.WiseSupplier;

class ConnectionPool implements WiseSupplier<Connection> {

/** The cached connections for each url. */
private static final Map<String, Connection> SINGLETONS = new ConcurrentHashMap();

/** The address. */
private final String url;

Expand Down Expand Up @@ -72,6 +76,9 @@ class ConnectionPool implements WiseSupplier<Connection> {
/** The actual connection pool. */
private final Set<ManagedConnection> busy;

/** The connection type. */
private final boolean singleton;

private ConnectionPool(String url) {
this.url = url;
this.dialect = detectDialect(url);
Expand All @@ -81,6 +88,7 @@ private ConnectionPool(String url) {
this.readOnly = config("typewriter.connection.readOnly", false);
this.timeout = config("typewriter.connection.timeout", 1000 * 30L);
this.isolation = config("typewriter.connection.isolation", -1);
this.singleton = config("typewriter.connection.singleton", false);
this.idles = new ArrayBlockingQueue(max);
this.busy = ConcurrentHashMap.newKeySet();

Expand Down Expand Up @@ -221,9 +229,11 @@ private class ManagedConnection implements Connection {
* @param delegation
* @throws SQLException
*/
@SuppressWarnings("resource")
private ManagedConnection() {
try {
this.delegation = dialect.createConnection(url, null);
this.delegation = !singleton ? dialect.createConnection(url, null)
: SINGLETONS.computeIfAbsent(url, (WiseFunction<String, Connection>) key -> dialect.createConnection(key, null));
this.delegation.setAutoCommit(autoCommit);
this.delegation.setReadOnly(readOnly);
if (0 <= isolation) this.delegation.setTransactionIsolation(isolation);
Expand All @@ -232,10 +242,6 @@ private ManagedConnection() {
}
}

private ManagedConnection(Connection delegation) {
this.delegation = delegation;
}

/**
* Manage any resource.
*
Expand Down
28 changes: 9 additions & 19 deletions src/main/java/typewriter/sqlite/SQLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,18 @@

public class SQLite extends Dialect {

/**
* The cached connections for each url.
*
* Since the SQLite developers seem to think that threads are evil, you would be better off
* having one thread handle all your database operations and serialize DB tasks on your own
* using Java code.
*/
private static final Map<String, Connection> CONNECTIONS = new ConcurrentHashMap();

/** The compiled regular expression manager. */
private static final Map<String, Pattern> REGEX = new ConcurrentHashMap();

/** The JAVA-SQL type mapping. */
private static final Map<Class, String> TYPES = new HashMap();

static {
// Since the SQLite developers seem to think that threads are evil, you would be better off
// having one thread handle all your database operations and serialize DB tasks on your own
// using Java code.
I.env("typewriter.connection.singleton.sqlite", true);

TYPES.put(int.class, "integer");
TYPES.put(long.class, "integer");
TYPES.put(float.class, "real");
Expand Down Expand Up @@ -94,18 +90,12 @@ public String types(Class type) {
*/
@Override
public Connection createConnection(String url, Properties properties) throws Exception {
return CONNECTIONS.computeIfAbsent(url, key -> {
try {
Connection connection = super.createConnection(url, Lazy.CONFIG.toProperties());
Connection connection = super.createConnection(url, Lazy.CONFIG.toProperties());

// register extra functions
Function.create(connection, "REGEXP", Lazy.REGEXP_FUNCTION);
// register extra functions
Function.create(connection, "REGEXP", Lazy.REGEXP_FUNCTION);

return connection;
} catch (Exception e) {
throw I.quiet(e);
}
});
return connection;
}

/**
Expand Down

0 comments on commit 40e6a53

Please sign in to comment.