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
DatabaseService
SQLContext
using the default ConfigService
getSQLContext
in interface DatabaseService
SQLContext
to databasejava.sql.SQLException
public final SQLContext getSQLContext(java.lang.String tag) throws java.sql.SQLException
DatabaseService
SQLContext
If the tag is null it returns the same SQLContext
as
DatabaseService.getSQLContext()
.getSQLContext
in interface DatabaseService
java.sql.SQLException
public SQLContext getSQLContext(java.lang.String tag, java.util.Properties props) throws java.sql.SQLException
getSQLContext
in interface DatabaseService
SQLContext
java.sql.SQLException
protected void setSQLContext(SQLContext c) throws java.sql.SQLException
c
- the Connection to usejava.sql.SQLException
protected 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.SQLException
public AppContext getContext()
getContext
in interface Contexed
public void cleanup()
AppContextCleanup
AppContext
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 AppContextCleanup
public void closeRetainedClosables()
closeRetainedClosables
in interface CloseRegistry
public java.lang.Class<DatabaseService> getType()
AppContextService
getType
in interface AppContextService<DatabaseService>
public boolean inTransaction()
DatabaseService
inTransaction
in interface DatabaseService
public void startTransaction()
DatabaseService
DatabaseService.inTransaction()
if possible doubt.startTransaction
in interface DatabaseService
public int getTargetIsolationLevel()
public void rollbackTransaction()
DatabaseService
rollbackTransaction
in interface DatabaseService
public void commitTransaction()
DatabaseService
CleanupService
unless the transaction phases all
make sense in their own right.commitTransaction
in interface DatabaseService
public void stopTransaction()
DatabaseService
stopTransaction
in interface DatabaseService
public int transactionStage()
DatabaseService
DatabaseService.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 DatabaseService
public void handleError(java.lang.String message, java.sql.SQLException e) throws DataFault
DatabaseService
DataFault
however
this also allow special handing of certain classes of error such as SQLTransientException
shandleError
in interface DatabaseService
DataFault
public void logError(java.lang.String message, java.sql.SQLException e)
DatabaseService
SQLException
directly. Normally this will just log the error however
this also allow special handing of certain classes of error such as SQLTransientException
slogError
in interface DatabaseService
public static int parseLevel(java.lang.String name)
public void addClosable(java.lang.AutoCloseable c)
addClosable
in interface CloseRegistry
public void removeClosable(java.lang.AutoCloseable c)
removeClosable
in interface CloseRegistry
public java.util.Map<java.lang.String,java.lang.Object> getConnectionAttributes() throws java.lang.Exception
DatabaseService
getConnectionAttributes
in interface DatabaseService
java.lang.Exception