public class DefaultDataBaseService extends java.lang.Object implements DatabaseService
DatabaseService
This gets connection parameters from the ConfigService but this is only queried
at the point where a database connection is required.
The following config parameters are read:
| Modifier and Type | Field and Description |
|---|---|
protected static java.lang.String |
POSTGRESQL_TYPE |
static Feature |
TRANSACTIONS_FEATURE |
static Feature |
TRANSACTIONS_ROLLBACK_TRANSIENT_ERRORS |
static Feature |
TRANSACTIONS_SERIALIZE_FEATURE |
LOG_INSERT_FEATURE, LOG_QUERY_FEATURE, LOG_UPDATE, USE_SQL_DISTICT_FEATURE| Constructor and Description |
|---|
DefaultDataBaseService(AppContext ctx) |
| Modifier and Type | Method and Description |
|---|---|
void |
addClosable(java.lang.AutoCloseable c) |
void |
cleanup()
AppContext is being closed. |
void |
closeRetainedClosables() |
void |
commitTransaction()
flush changes since start of last commit.
|
java.util.Map<java.lang.String,java.lang.Object> |
getConnectionAttributes()
get a map of information about the connection
|
AppContext |
getContext() |
SQLContext |
getSQLContext()
get
SQLContext using the default ConfigService |
SQLContext |
getSQLContext(java.lang.String tag)
get alternative database
SQLContext
If the tag is null it returns the same SQLContext as
DatabaseService.getSQLContext(). |
SQLContext |
getSQLContext(java.lang.String tag,
java.util.Properties props)
returns a database connection for the application based on a set of Config Propertis
|
int |
getTargetIsolationLevel() |
java.lang.Class<DatabaseService> |
getType()
Returns the type of service the class should be registered under.
|
void |
handleError(java.lang.String message,
java.sql.SQLException e)
Handle an unexpected database exception.
|
boolean |
inTransaction()
Are we already in a transaction
|
void |
logError(java.lang.String message,
java.sql.SQLException e)
handle an
SQLException directly. |
protected SQLContext |
makeSQLContext(java.lang.String tag,
java.util.Properties props)
default method for creating a
SQLContext from InitParameters
This method is only invoked the first time an AppContext needs a Connection
to reduce overhead in static pages. |
static int |
parseLevel(java.lang.String name) |
void |
removeClosable(java.lang.AutoCloseable c) |
void |
rollbackTransaction()
Abort changes since start or last commit
|
protected void |
setSQLContext(SQLContext c)
Set the database Connection.
|
void |
startTransaction()
Start a database transaction
Transactions cannot be nested so use
DatabaseService.inTransaction() if possible doubt. |
void |
stopTransaction()
finish transaction (includes commit).
|
int |
transactionStage()
returns the number of times
DatabaseService.commitTransaction() has been called
since the last transaction start. |
public static final Feature TRANSACTIONS_FEATURE
public static final Feature TRANSACTIONS_SERIALIZE_FEATURE
public static final Feature TRANSACTIONS_ROLLBACK_TRANSIENT_ERRORS
protected static final java.lang.String POSTGRESQL_TYPE
public DefaultDataBaseService(AppContext ctx)
public final SQLContext getSQLContext() throws java.sql.SQLException
DatabaseServiceSQLContext using the default ConfigServicegetSQLContext in interface DatabaseServiceSQLContext to databasejava.sql.SQLExceptionpublic final SQLContext getSQLContext(java.lang.String tag) throws java.sql.SQLException
DatabaseServiceSQLContext
If the tag is null it returns the same SQLContext as
DatabaseService.getSQLContext().getSQLContext in interface DatabaseServicejava.sql.SQLExceptionpublic SQLContext getSQLContext(java.lang.String tag, java.util.Properties props) throws java.sql.SQLException
getSQLContext in interface DatabaseServiceSQLContextjava.sql.SQLExceptionprotected void setSQLContext(SQLContext c) throws java.sql.SQLException
c - the Connection to usejava.sql.SQLExceptionprotected SQLContext makeSQLContext(java.lang.String tag, java.util.Properties props) throws java.sql.SQLException
SQLContext from InitParameters
This method is only invoked the first time an AppContext needs a Connection
to reduce overhead in static pages.java.sql.SQLExceptionpublic AppContext getContext()
getContext in interface Contexedpublic void cleanup()
AppContextCleanupAppContext is being closed.
Only use this for cleanup that can't be handled by
normal garbage collection or for state which is never returned by reference.cleanup in interface AppContextCleanuppublic void closeRetainedClosables()
closeRetainedClosables in interface CloseRegistrypublic java.lang.Class<DatabaseService> getType()
AppContextServicegetType in interface AppContextService<DatabaseService>public boolean inTransaction()
DatabaseServiceinTransaction in interface DatabaseServicepublic void startTransaction()
DatabaseServiceDatabaseService.inTransaction() if possible doubt.startTransaction in interface DatabaseServicepublic int getTargetIsolationLevel()
public void rollbackTransaction()
DatabaseServicerollbackTransaction in interface DatabaseServicepublic void commitTransaction()
DatabaseServiceCleanupService unless the transaction phases all
make sense in their own right.commitTransaction in interface DatabaseServicepublic void stopTransaction()
DatabaseServicestopTransaction in interface DatabaseServicepublic int transactionStage()
DatabaseServiceDatabaseService.commitTransaction() has been called
since the last transaction start.
If this returns 0 then a roll-back will return to the state at DatabaseService.startTransaction() if greater than zero
there have been intermediate commits.
To aid in testing the value is not reset to zero until the next call to DatabaseService.startTransaction()transactionStage in interface DatabaseServicepublic void handleError(java.lang.String message,
java.sql.SQLException e)
throws DataFault
DatabaseServiceDataFault however
this also allow special handing of certain classes of error such as SQLTransientExceptionshandleError in interface DatabaseServiceDataFaultpublic void logError(java.lang.String message,
java.sql.SQLException e)
DatabaseServiceSQLException directly. Normally this will just log the error however
this also allow special handing of certain classes of error such as SQLTransientExceptionslogError in interface DatabaseServicepublic static int parseLevel(java.lang.String name)
public void addClosable(java.lang.AutoCloseable c)
addClosable in interface CloseRegistrypublic void removeClosable(java.lang.AutoCloseable c)
removeClosable in interface CloseRegistrypublic java.util.Map<java.lang.String,java.lang.Object> getConnectionAttributes()
throws java.lang.Exception
DatabaseServicegetConnectionAttributes in interface DatabaseServicejava.lang.Exception