Class Session
java.lang.Object
net.sf.jailer.database.Session
Manages database sessions on a 'per thread' basis.
Executes SQL-Statements in the context of a session.
- Author:
- Ralf Wisser
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classReads a JDBC-result-set.static interfaceConnection factory.static interfaceReads a JDBC-result-set. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.slf4j.LoggerThe logger.protected ThreadLocal<Connection> Hold a connection for each thread.protected final Session.ConnectionFactoryConnection factory.final DBMSThe DBMS.final StringThe dbUrl (nullif unknown)protected AtomicBooleanfinal StringThe DBMS.static longfinal Objectfinal WorkingTableScopeScope of temporary tables. -
Constructor Summary
ConstructorsConstructorDescriptionSession(DataSource dataSource, DBMS dbms, Integer isolationLevel) Constructor.Session(DataSource dataSource, DBMS dbms, Integer isolationLevel, WorkingTableScope scope, boolean transactional) Constructor.Session(DataSource dataSource, DBMS dbms, Integer isolationLevel, WorkingTableScope scope, boolean transactional, boolean local) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionbooleancheckQuery(String sql) Checks whether a SQL query can be executed without errors.voidCommits all connections.Create a new connection.voidDisables re-validation of cached database meta data.longExecutes a SQL-Statement.longExecutes a SQL-Statement.voidexecuteQuery(File sqlFile, Session.ResultSetReader reader, boolean withExplicitCommit) Executes a SQL-Query (SELECT).longexecuteQuery(String sqlQuery, Session.ResultSetReader reader) Executes a SQL-Query (SELECT).longexecuteQuery(String sqlQuery, Session.ResultSetReader reader, boolean withExplicitCommit) Executes a SQL-Query (SELECT).longexecuteQuery(String sqlQuery, Session.ResultSetReader reader, String alternativeSQL, Object context, long limit) Executes a SQL-Query (SELECT).longexecuteQuery(String sqlQuery, Session.ResultSetReader reader, String alternativeSQL, Object context, long limit, boolean withExplicitCommit) Executes a SQL-Query (SELECT).longexecuteQuery(String sqlQuery, Session.ResultSetReader reader, String alternativeSQL, Object context, long limit, int timeout, boolean withExplicitCommit) Executes a SQL-Query (SELECT) with timeout.intexecuteUpdate(String sqlUpdate) Executes a SQL-Update (INSERT, DELETE or UPDATE).intexecuteUpdate(String sqlUpdate, Object[] parameter) Executes a SQL-Update (INSERT, DELETE or UPDATE) with parameters.Gets CLI connection arguments (UI support).Gets the connection for the current thread.Returns a string summarising connection pool statistics.Returns a suitableInlineViewStylefor this session.Gets optional schema for database analysis.booleanReturns whether SQL statements should be logged.Gets DB meta data.Gets connection password (UI support).Gets DB schema name.getSessionProperty(Class<?> owner, String name) Gets a session property.booleanNo SQL-Exceptions will be logged in silent mode.protected voidinit()voidinsertBlob(String table, String column, String where, File lobFile) Inserts a BLob.voidinsertClob(String table, String column, String where, File lobFile, long length) Inserts a CLob.voidinsertSQLXML(String table, String column, String where, File lobFile, long length) Inserts a SQL-XML.booleanReturns whether the current thread holds the connection exclusively.booleanisDown()Returns whether this session has been shut down.voidCancels all currently running statements.voidMarks a connection as potentially invalid.voidCloses current connection and opens a new one.voidremoveSessionProperties(Class<?> owner) Removes all session properties.static voidClears the global fallback connection.voidRolls back and closes all connections.voidsetCliArguments(List<String> args) Sets CLI connection arguments (UI support).protected voidsetConnection(Connection con) voidPromotes the current default connection to the global fallback connection.voidsetIntrospectionSchema(String introspectionSchema) Sets optional schema for database analysis.voidsetLogPrefix(String logPrefix) Sets a prefix string that is prepended to all log messages from this session.voidsetLogStatements(boolean logStatements) Sets whether SQL statements should be logged.voidsetPassword(String password) Sets connection password (UI support).voidsetSessionProperty(Class<?> owner, String name, Object property) Sets a session property.voidsetSilent(boolean silent) No SQL-Exceptions will be logged in silent mode.static voidMarks the current thread as sharing a connection with other threads.voidshutDown()Closes all connections.
-
Field Details
-
connection
Hold a connection for each thread. -
scope
Scope of temporary tables. -
_log
public static final org.slf4j.Logger _logThe logger. -
connectionFactory
Connection factory. -
dbms
The DBMS. -
driverClassName
The DBMS. -
dbUrl
The dbUrl (nullif unknown) -
down
-
MD_GETCOLUMNS_LOCK
-
lastUpdateTS
public static volatile long lastUpdateTS
-
-
Constructor Details
-
Session
Constructor.- Parameters:
dataSource- the data sourcedbms- the DBMSisolationLevel- the transaction isolation level, ornullto use the default- Throws:
SQLException
-
Session
public Session(DataSource dataSource, DBMS dbms, Integer isolationLevel, WorkingTableScope scope, boolean transactional) throws SQLException Constructor.- Parameters:
dataSource- the data sourcedbms- the DBMSisolationLevel- the transaction isolation level, ornullto use the defaultscope- the working-table scopetransactional-trueto operate in transactional mode- Throws:
SQLException
-
Session
public Session(DataSource dataSource, DBMS dbms, Integer isolationLevel, WorkingTableScope scope, boolean transactional, boolean local) throws SQLException Constructor.- Parameters:
dataSource- the data sourcedbms- the DBMSisolationLevel- the transaction isolation level, ornullto use the defaultscope- the working-table scopetransactional-trueto operate in transactional modelocal-truefor the local entity-graph database- Throws:
SQLException
-
-
Method Details
-
setConnection
-
markConnectionAsPotentiallyInvalid
Marks a connection as potentially invalid. Forces to re-validate the connection ingetConnection().- Parameters:
con- the connection
-
init
- Throws:
SQLException
-
reconnect
public void reconnect()Closes current connection and opens a new one. -
setSilent
public void setSilent(boolean silent) No SQL-Exceptions will be logged in silent mode.- Parameters:
silent-truefor silence
-
getSilent
public boolean getSilent()No SQL-Exceptions will be logged in silent mode.- Returns:
truefor silence
-
setLogStatements
public void setLogStatements(boolean logStatements) Sets whether SQL statements should be logged.- Parameters:
logStatements-trueto enable statement logging
-
getLogStatements
public boolean getLogStatements()Returns whether SQL statements should be logged.- Returns:
trueif statement logging is enabled
-
getSchema
-
executeQuery
public long executeQuery(String sqlQuery, Session.ResultSetReader reader, boolean withExplicitCommit) throws SQLException Executes a SQL-Query (SELECT).- Parameters:
sqlQuery- the query in SQLreader- the reader for the resultwithExplicitCommit- iftrue, switch of autocommit and commit explicitly- Throws:
SQLException
-
executeQuery
Executes a SQL-Query (SELECT).- Parameters:
sqlQuery- the query in SQLreader- the reader for the result- Throws:
SQLException
-
executeQuery
public long executeQuery(String sqlQuery, Session.ResultSetReader reader, String alternativeSQL, Object context, long limit, boolean withExplicitCommit) throws SQLException Executes a SQL-Query (SELECT).- Parameters:
sqlQuery- the query in SQLreader- the reader for the resultalternativeSQL- query to be executed if sqlQuery failscontext- cancellation contextlimit- row limit, 0 for unlimitedwithExplicitCommit- iftrue, switch of autocommit and commit explicitly- Throws:
SQLException
-
executeQuery
public long executeQuery(String sqlQuery, Session.ResultSetReader reader, String alternativeSQL, Object context, long limit) throws SQLException Executes a SQL-Query (SELECT).- Parameters:
sqlQuery- the query in SQLreader- the reader for the resultalternativeSQL- query to be executed if sqlQuery failscontext- cancellation contextlimit- row limit, 0 for unlimited- Throws:
SQLException
-
executeQuery
public long executeQuery(String sqlQuery, Session.ResultSetReader reader, String alternativeSQL, Object context, long limit, int timeout, boolean withExplicitCommit) throws SQLException Executes a SQL-Query (SELECT) with timeout.- Parameters:
sqlQuery- the query in SQLreader- the reader for the resultalternativeSQL- query to be executed if sqlQuery failscontext- cancellation contextlimit- row limit, 0 for unlimitedtimeout- the timeout in secwithExplicitCommit- iftrue, switch of autocommit and commit explicitly- Throws:
SQLException
-
executeQuery
public void executeQuery(File sqlFile, Session.ResultSetReader reader, boolean withExplicitCommit) throws SQLException Executes a SQL-Query (SELECT).- Parameters:
sqlFile- file containing a query in SQLreader- the reader for the resultwithExplicitCommit- iftrue, switch of autocommit and commit explicitly- Throws:
SQLException
-
executeUpdate
Executes a SQL-Update (INSERT, DELETE or UPDATE).- Parameters:
sqlUpdate- the update in SQL- Returns:
- update-count
- Throws:
SQLException
-
executeUpdate
Executes a SQL-Update (INSERT, DELETE or UPDATE) with parameters.- Parameters:
sqlUpdate- the update in SQLparameter- the parameters- Returns:
- update-count
- Throws:
SQLException
-
insertClob
public void insertClob(String table, String column, String where, File lobFile, long length) throws SQLException, IOException Inserts a CLob.- Parameters:
table- the target table namecolumn- the target column namewhere- the WHERE clause identifying the row to updatelobFile- the file containing the CLOB datalength- the length of the CLOB data in characters- Throws:
SQLExceptionIOException
-
insertSQLXML
public void insertSQLXML(String table, String column, String where, File lobFile, long length) throws SQLException, IOException Inserts a SQL-XML.- Parameters:
table- the target table namecolumn- the target column namewhere- the WHERE clause identifying the row to updatelobFile- the file containing the XML datalength- the length of the XML data in characters- Throws:
SQLExceptionIOException
-
insertBlob
public void insertBlob(String table, String column, String where, File lobFile) throws SQLException, IOException Inserts a BLob.- Parameters:
table- the target table namecolumn- the target column namewhere- the WHERE clause identifying the row to updatelobFile- the file containing the BLOB data- Throws:
SQLExceptionIOException
-
execute
Executes a SQL-Statement.- Parameters:
sql- the SQL-Statement- Returns:
- the update count
- Throws:
SQLException
-
execute
public long execute(String sql, Object cancellationContext, boolean acceptQueries) throws SQLException Executes a SQL-Statement.- Parameters:
sql- the SQL-StatementcancellationContext- the context used to check for cancellation, ornullacceptQueries- iftrue, SELECT statements are also accepted- Returns:
- the update count, or 0 for queries
- Throws:
SQLException
-
disableMetaDataChecking
public void disableMetaDataChecking()Disables re-validation of cached database meta data. -
getMetaData
Gets DB meta data.- Returns:
- DB meta data
- Throws:
SQLException
-
shutDown
public void shutDown()Closes all connections. -
isDown
public boolean isDown()Returns whether this session has been shut down.- Returns:
trueif the session is shut down
-
killRunningStatements
public void killRunningStatements()Cancels all currently running statements. -
rollbackAll
Rolls back and closes all connections.- Throws:
SQLException
-
commitAll
-
getIntrospectionSchema
Gets optional schema for database analysis.- Returns:
- optional schema for database analysis
-
setIntrospectionSchema
Sets optional schema for database analysis.- Parameters:
introspectionSchema- optional schema for database analysis
-
getPassword
-
setPassword
Sets connection password (UI support).- Parameters:
password- the connection password
-
setCliArguments
-
getCliArguments
-
getConnection
Gets the connection for the current thread.- Returns:
- the connection for the current thread
- Throws:
SQLException
-
createNewConnection
Create a new connection. Must be closed from caller.- Returns:
- a new connection
- Throws:
SQLException
-
getInlineViewStyle
Returns a suitableInlineViewStylefor this session.- Returns:
- a suitable
InlineViewStylefor this session ornull, if no style is found
-
setSessionProperty
-
getSessionProperty
-
removeSessionProperties
Removes all session properties.- Parameters:
owner- the class that owns the properties
-
checkQuery
Checks whether a SQL query can be executed without errors.- Parameters:
sql- the SQL query to check- Returns:
trueif the query is executable without errors
-
setLogPrefix
Sets a prefix string that is prepended to all log messages from this session.- Parameters:
logPrefix- the log prefix to set
-
setGlobalFallbackConnection
public void setGlobalFallbackConnection()Promotes the current default connection to the global fallback connection. -
resetGlobalFallbackConnection
public static void resetGlobalFallbackConnection()Clears the global fallback connection. -
isConnectionExclusive
public boolean isConnectionExclusive()Returns whether the current thread holds the connection exclusively.- Returns:
trueif no other thread is using the same connection
-
getConnectionStats
Returns a string summarising connection pool statistics.- Returns:
- connection statistics string
-