base.SingleConnectionDataSource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of base Show documentation
Show all versions of base Show documentation
A collection of basic java utility classes that provide basic features for a standalone/simple JEE application. Backed by a Cassandra, MySQL, or SQLite database, it provides, web page templates, user and group management, and a searchable online audit log of all user activity.
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
package base;
import java.io.PrintWriter;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
import javax.sql.DataSource;
/**
* A datasource that holds a single database connection. Serialises requests to an SQLite database
* and avoids the SQLite "locked" exception.
*/
public class SingleConnectionDataSource implements DataSource {
private SC c = null;
public SingleConnectionDataSource(Connection c) {
this.c = new SC(c);
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return null;
}
@Override
public int getLoginTimeout() throws SQLException {
return 0;
}
@Override
public void setLogWriter(PrintWriter arg0) throws SQLException {
}
@Override
public void setLoginTimeout(int arg0) throws SQLException {
}
@Override
public boolean isWrapperFor(Class arg0) throws SQLException {
return false;
}
@Override
public T unwrap(Class arg0) throws SQLException {
return null;
}
@Override
public Connection getConnection() throws SQLException {
try {
c.lock();
c.setAutoCommit(false);
} catch (InterruptedException e) {
throw new SQLException(e);
}
return c;
}
@Override
public Connection getConnection(String arg0, String arg1) throws SQLException {
return getConnection();
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
/**
* Wrapper around a database connection prevents closing of a connection. lock()
is invoked
* prior to this connection being passed out, and unlock()
is automatically called when
* close()
is requested.
*/
public class SC implements Connection {
private Connection c;
private final Lock lock = new ReentrantLock();
public SC(Connection c) {
this.c = c;
}
public void lock() throws InterruptedException {
lock.lockInterruptibly();
}
public void releaseLock() {
lock.unlock();
}
@Override
public boolean isWrapperFor(Class iface) throws SQLException {
return false;
}
@Override
public T unwrap(Class iface) throws SQLException {
return null;
}
@Override
public void clearWarnings() throws SQLException {
c.clearWarnings();
}
@Override
public void close() throws SQLException {
releaseLock();
}
@Override
public void commit() throws SQLException {
c.commit();
}
@Override
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
return c.createArrayOf(typeName, elements);
}
@Override
public Blob createBlob() throws SQLException {
return c.createBlob();
}
@Override
public Clob createClob() throws SQLException {
return c.createClob();
}
@Override
public NClob createNClob() throws SQLException {
c.createNClob();
return null;
}
@Override
public SQLXML createSQLXML() throws SQLException {
return c.createSQLXML();
}
@Override
public Statement createStatement() throws SQLException {
return c.createStatement();
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
return c.createStatement(resultSetType, resultSetConcurrency);
}
@Override
public Statement createStatement(int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
return c.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
}
@Override
public Struct createStruct(String typeName, Object[] attributes)
throws SQLException {
return c.createStruct(typeName, attributes);
}
@Override
public boolean getAutoCommit() throws SQLException {
return c.getAutoCommit();
}
@Override
public String getCatalog() throws SQLException {
return c.getCatalog();
}
@Override
public Properties getClientInfo() throws SQLException {
return c.getClientInfo();
}
@Override
public String getClientInfo(String name) throws SQLException {
return c.getClientInfo(name);
}
@Override
public int getHoldability() throws SQLException {
return c.getHoldability();
}
@Override
public DatabaseMetaData getMetaData() throws SQLException {
return c.getMetaData();
}
@Override
public int getTransactionIsolation() throws SQLException {
return c.getTransactionIsolation();
}
@Override
public Map> getTypeMap() throws SQLException {
return c.getTypeMap();
}
@Override
public SQLWarning getWarnings() throws SQLException {
return c.getWarnings();
}
@Override
public boolean isClosed() throws SQLException {
return c.isClosed();
}
@Override
public boolean isReadOnly() throws SQLException {
return c.isReadOnly();
}
@Override
public boolean isValid(int timeout) throws SQLException {
return c.isValid(timeout);
}
@Override
public String nativeSQL(String sql) throws SQLException {
return c.nativeSQL(sql);
}
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
return c.prepareCall(sql);
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return c.prepareCall(sql, resultSetType, resultSetConcurrency);
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
return c.prepareCall(sql);
}
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
return c.prepareStatement(sql);
}
@Override
public PreparedStatement prepareStatement(String sql,
int autoGeneratedKeys) throws SQLException {
return c.prepareStatement(sql, autoGeneratedKeys);
}
@Override
public PreparedStatement prepareStatement(String sql,
int[] columnIndexes) throws SQLException {
return c.prepareStatement(sql, columnIndexes);
}
@Override
public PreparedStatement prepareStatement(String sql,
String[] columnNames) throws SQLException {
return c.prepareStatement(sql, columnNames);
}
@Override
public PreparedStatement prepareStatement(String sql,
int resultSetType, int resultSetConcurrency)
throws SQLException {
return c.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
@Override
public PreparedStatement prepareStatement(String sql,
int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
return c.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
c.releaseSavepoint(savepoint);
}
@Override
public void rollback() throws SQLException {
c.rollback();
}
@Override
public void rollback(Savepoint savepoint) throws SQLException {
c.rollback(savepoint);
}
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
c.setAutoCommit(autoCommit);
}
@Override
public void setCatalog(String catalog) throws SQLException {
c.setCatalog(catalog);
}
@Override
public void setClientInfo(Properties properties) throws SQLClientInfoException {
c.setClientInfo(properties);
}
@Override
public void setClientInfo(String name, String value) throws SQLClientInfoException {
c.setClientInfo(name, value);
}
@Override
public void setHoldability(int holdability) throws SQLException {
c.setHoldability(holdability);
}
@Override
public void setReadOnly(boolean readOnly) throws SQLException {
throw new IllegalStateException("read only not allowed");
}
@Override
public Savepoint setSavepoint() throws SQLException {
return c.setSavepoint();
}
@Override
public Savepoint setSavepoint(String name) throws SQLException {
return c.setSavepoint(name);
}
@Override
public void setTransactionIsolation(int level) throws SQLException {
c.setTransactionIsolation(level);
}
@Override
public void setTypeMap(Map> map) throws SQLException {
c.setTypeMap(map);
}
@Override
public void setSchema(String schema) throws SQLException {
c.setSchema(schema);
}
@Override
public String getSchema() throws SQLException {
return c.getSchema();
}
@Override
public void abort(Executor executor) throws SQLException {
c.abort(executor);
}
@Override
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
c.setNetworkTimeout(executor, milliseconds);
}
@Override
public int getNetworkTimeout() throws SQLException {
return c.getNetworkTimeout();
}
}
}