Package fr.dyade.aaa.util
Class DBTransaction
- java.lang.Object
-
- fr.dyade.aaa.util.BaseTransaction
-
- fr.dyade.aaa.util.AbstractTransaction
-
- fr.dyade.aaa.util.DBTransaction
-
- All Implemented Interfaces:
DBTransactionMBean,Transaction,TransactionMBean
- Direct Known Subclasses:
DerbyDBTransaction,JDBCTransaction
public abstract class DBTransaction extends AbstractTransaction implements DBTransactionMBean
The DBTransaction class implements an atomic storage through a JDBC interface. This class is designed to be specialized for different database implementation.Be Careful, the configuration properties don't work for the transaction component: these properties are saved in the transaction repository so they can not be used to configure it.
- See Also:
Transaction,MySQLDBTransaction,DerbyDBTransaction
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class fr.dyade.aaa.util.AbstractTransaction
AbstractTransaction.Context
-
-
Field Summary
Fields Modifier and Type Field Description (package private) intbadinserts(package private) intbadupdatesprivate longcommitBytesNumber of bytes written in commit.private intcommitCountNumber of commit operation since starting up.protected Connectionconnprotected intconnectRetryCountprotected longconnectRetryMaxPeriodprotected longconnectRetryMinDelaystatic StringDB_TRANSACTION_PREFIXprotected Stringdbcloseprotected Stringdbdeleteprotected Stringdbinsertprotected Stringdbloadprotected Stringdbtableprotected Stringdbupdateprivate PreparedStatementdeleteStmtstatic StringDFLT_TABLE_PREFIXprivate PreparedStatementinsertStmtprotected intJDBC_CONNECT_RETRY_COUNT_DFLTprotected longJDBC_CONNECT_RETRY_MAX_PERIOD_DFLTprotected longJDBC_CONNECT_RETRY_MIN_DELAY_DFLTprivate PreparedStatementloadStmt(package private) static intlogThresholdOperationNumber of pooled operation, by default 1000.(package private) intnbdeletes(package private) intnbinserts(package private) intnbnoop(package private) intnbupdatesstatic StringTABLE_NAME_PROPprivate PreparedStatementupdateStmt-
Fields inherited from class fr.dyade.aaa.util.AbstractTransaction
average, dir, logmon, MONITOR, OOS_STREAM_HEADER, PERIODS, perThreadContext, phase, startTime
-
Fields inherited from class fr.dyade.aaa.util.BaseTransaction
DEBUG
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedDBTransaction()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Stringbackup(String path)Backups the content of Transaction module.voidclose()Close the transaction module.voidcommit(boolean release)Commit the current transaction.protected abstract voidconnectDB()This method try to reconnect to the database, it is used in case of failure during commit.private voidcreatePreparedStatement()private voiddbLogCommit(Hashtable<String,DBOperation> log)Executes all SQL statements corresponding to log operations, and the commit.private voiddbLogExecute(Hashtable<String,DBOperation> log)Executes all SQL statements corresponding to log operations.voiddelete(String dirName, String name)Deletes the specified object.StringdumpProperties()Returns the configuration properties of the component.protected voiddumpProperties(StringBuilder strbuf)(package private) Stringfname(String dirName, String name)intgetBadInserts()Returns the number of bad inserts in database since last boot (record already existing).intgetBadUpdates()Returns the number of bad updates in database since last boot (record needing to be inserted)..longgetCommitBytes()Returns the number of bytes written to database since last boot.intgetCommitCount()Returns the number of commit operation since last boot.StringgetDBCloseStatement()StringgetDBDeleteStatement()StringgetDBInsertStatement()StringgetDBLoadStatement()StringgetDBTableName()StringgetDBUpdateStatement()byte[]getFromLog(String name)String[]getList(String prefix)Returns an array of strings naming the persistent objects denoted by a name that satisfy the specified prefix.intgetLogThresholdOperation()Returns the pool size foroperationobjects, by default 1000.intgetNbDeletes()Returns the number of deletes in database since last boot.intgetNbInserts()Returns the number of inserts in database since last boot.intgetNbUpdates()Returns the number of updates in database since last boot.StringgetPersistenceDir()Returns the path of persistence directory.protected abstract voidinitDB()Instantiates the database driver and creates the table if necessaryvoidinitRepository()voidloadAll(String prefix, Map map)Fills the map with all objects of the component whose name begins with the prefix.byte[]loadByteArray(String dirName, String name)Load the specified byte array.protected voidsaveInLog(byte[] buf, String dirName, String name, Hashtable log, boolean copy, boolean first)Register the specified object in transaction log.protected voidsetPhase(int newPhase)Changes the transaction state.voidstop()Stops the transaction module.booleanuseLoadAll()Returns true if this Transaction implementation implements an optimized loadAll method.-
Methods inherited from class fr.dyade.aaa.util.AbstractTransaction
begin, containsOperations, create, create, createByteArray, createByteArray, delete, getAverageCommitTime, getOperationCount, getPhase, getPhaseInfo, getStartTime, init, isPersistent, load, load, loadByteArray, loadFromByteArray, release, save, save, save, saveByteArray, saveByteArray, saveByteArray
-
Methods inherited from class fr.dyade.aaa.util.BaseTransaction
dumpObjectList, freeze, getBoolean, getInteger, getInteger, getObject, getObjectList, getProperty, getProperty, loadProperties, saveProperties
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface fr.dyade.aaa.util.DBTransactionMBean
getAverageCommitTime, getPhase, getPhaseInfo
-
Methods inherited from interface fr.dyade.aaa.util.TransactionMBean
dumpObjectList, getObject, getObjectList, getStartTime, isPersistent
-
-
-
-
Field Detail
-
DB_TRANSACTION_PREFIX
public static final String DB_TRANSACTION_PREFIX
- See Also:
- Constant Field Values
-
TABLE_NAME_PROP
public static final String TABLE_NAME_PROP
- See Also:
- Constant Field Values
-
DFLT_TABLE_PREFIX
public static final String DFLT_TABLE_PREFIX
- See Also:
- Constant Field Values
-
dbtable
protected String dbtable
-
logThresholdOperation
static int logThresholdOperation
Number of pooled operation, by default 1000. This value can be adjusted for a particular server by settingDBLogThresholdOperationspecific property.This property can be fixed only from
javalaunching command, or through System.property method.
-
conn
protected Connection conn
-
dbinsert
protected String dbinsert
-
dbupdate
protected String dbupdate
-
dbload
protected String dbload
-
dbdelete
protected String dbdelete
-
dbclose
protected String dbclose
-
insertStmt
private PreparedStatement insertStmt
-
updateStmt
private PreparedStatement updateStmt
-
loadStmt
private PreparedStatement loadStmt
-
deleteStmt
private PreparedStatement deleteStmt
-
commitCount
private int commitCount
Number of commit operation since starting up.
-
commitBytes
private long commitBytes
Number of bytes written in commit.
-
JDBC_CONNECT_RETRY_COUNT_DFLT
protected final int JDBC_CONNECT_RETRY_COUNT_DFLT
- See Also:
- Constant Field Values
-
connectRetryCount
protected int connectRetryCount
-
JDBC_CONNECT_RETRY_MAX_PERIOD_DFLT
protected final long JDBC_CONNECT_RETRY_MAX_PERIOD_DFLT
- See Also:
- Constant Field Values
-
connectRetryMaxPeriod
protected long connectRetryMaxPeriod
-
JDBC_CONNECT_RETRY_MIN_DELAY_DFLT
protected final long JDBC_CONNECT_RETRY_MIN_DELAY_DFLT
- See Also:
- Constant Field Values
-
connectRetryMinDelay
protected long connectRetryMinDelay
-
nbinserts
int nbinserts
-
badinserts
int badinserts
-
nbupdates
int nbupdates
-
badupdates
int badupdates
-
nbdeletes
int nbdeletes
-
nbnoop
int nbnoop
-
-
Method Detail
-
getDBTableName
public String getDBTableName()
- Specified by:
getDBTableNamein interfaceDBTransactionMBean
-
getLogThresholdOperation
public int getLogThresholdOperation()
Returns the pool size foroperationobjects, by default 1000.- Specified by:
getLogThresholdOperationin interfaceDBTransactionMBean- Returns:
- The pool size for
operationobjects.
-
getDBInsertStatement
public String getDBInsertStatement()
- Specified by:
getDBInsertStatementin interfaceDBTransactionMBean
-
getDBUpdateStatement
public String getDBUpdateStatement()
- Specified by:
getDBUpdateStatementin interfaceDBTransactionMBean
-
getDBLoadStatement
public String getDBLoadStatement()
- Specified by:
getDBLoadStatementin interfaceDBTransactionMBean
-
getDBDeleteStatement
public String getDBDeleteStatement()
- Specified by:
getDBDeleteStatementin interfaceDBTransactionMBean
-
getDBCloseStatement
public String getDBCloseStatement()
- Specified by:
getDBCloseStatementin interfaceDBTransactionMBean
-
initRepository
public void initRepository() throws IOException- Specified by:
initRepositoryin classAbstractTransaction- Throws:
IOException
-
createPreparedStatement
private void createPreparedStatement() throws IOException- Throws:
IOException
-
initDB
protected abstract void initDB() throws IOExceptionInstantiates the database driver and creates the table if necessary- Throws:
IOException- an error occurs.
-
getPersistenceDir
public String getPersistenceDir()
Returns the path of persistence directory.- Returns:
- The path of persistence directory.
-
setPhase
protected final void setPhase(int newPhase)
Description copied from class:AbstractTransactionChanges the transaction state.- Specified by:
setPhasein classAbstractTransaction- Parameters:
newPhase- the new transaction state.
-
getCommitCount
public final int getCommitCount()
Returns the number of commit operation since last boot.- Specified by:
getCommitCountin interfaceTransaction- Specified by:
getCommitCountin interfaceTransactionMBean- Returns:
- The number of commit operation.
-
getCommitBytes
public final long getCommitBytes()
Returns the number of bytes written to database since last boot.- Specified by:
getCommitBytesin interfaceDBTransactionMBean- Returns:
- The number of commit operation.
-
getList
public final String[] getList(String prefix)
Returns an array of strings naming the persistent objects denoted by a name that satisfy the specified prefix. Each string is an object name.- Specified by:
getListin interfaceTransaction- Parameters:
prefix- the prefix- Returns:
- An array of strings naming the persistent objects denoted by a name that satisfy the specified prefix. The array will be empty if no names match.
-
getFromLog
public byte[] getFromLog(String name) throws IOException
- Throws:
IOException
-
useLoadAll
public boolean useLoadAll()
Returns true if this Transaction implementation implements an optimized loadAll method.- Specified by:
useLoadAllin interfaceTransaction- Specified by:
useLoadAllin interfaceTransactionMBean- Overrides:
useLoadAllin classAbstractTransaction- Returns:
- false.
-
loadAll
public void loadAll(String prefix, Map map)
Fills the map with all objects of the component whose name begins with the prefix.- Specified by:
loadAllin interfaceTransaction- Overrides:
loadAllin classAbstractTransaction- Parameters:
prefix- The prefix of searched objects.map- The map of corresponding objects.
-
saveInLog
protected final void saveInLog(byte[] buf, String dirName, String name, Hashtable log, boolean copy, boolean first) throws IOExceptionDescription copied from class:AbstractTransactionRegister the specified object in transaction log.- Specified by:
saveInLogin classAbstractTransaction- Parameters:
buf- the byte array containing the state of the object.dirName- the directory name of the object.name- the name of the object.log- the log to use.copy- the byte array can be modified, copy it.first- the object is a new one.- Throws:
IOException- an error occurs.
-
loadByteArray
public byte[] loadByteArray(String dirName, String name) throws IOException
Description copied from interface:TransactionLoad the specified byte array.- Specified by:
loadByteArrayin interfaceTransaction- Parameters:
dirName- the directory name of the object.name- the name of the object.- Returns:
- the loaded byte array.
- Throws:
IOException- an error occurs.
-
delete
public void delete(String dirName, String name)
Description copied from interface:TransactionDeletes the specified object.- Specified by:
deletein interfaceTransaction- Parameters:
dirName- the directory name of the object.name- the name of the object.
-
connectDB
protected abstract void connectDB() throws IOExceptionThis method try to reconnect to the database, it is used in case of failure during commit. Be careful, unless a connection pool is used, we need to recreate the prepared statement.- Throws:
IOException- The reconnection has failed.
-
commit
public final void commit(boolean release) throws IOExceptionDescription copied from interface:TransactionCommit the current transaction.- Specified by:
commitin interfaceTransaction- Parameters:
release- if true releases the transaction at the end of the commit.- Throws:
IOException- an error occurs.
-
dbLogCommit
private void dbLogCommit(Hashtable<String,DBOperation> log) throws SQLException
Executes all SQL statements corresponding to log operations, and the commit.- Parameters:
log- Hashtable containing all operations to commit.- Throws:
SQLException- an error occurs.
-
getNbInserts
public final int getNbInserts()
Returns the number of inserts in database since last boot.- Specified by:
getNbInsertsin interfaceDBTransactionMBean- Returns:
- The number of inserts in database.
-
getBadInserts
public final int getBadInserts()
Returns the number of bad inserts in database since last boot (record already existing).- Specified by:
getBadInsertsin interfaceDBTransactionMBean- Returns:
- The number of bad insert in database.
-
getNbUpdates
public final int getNbUpdates()
Returns the number of updates in database since last boot.- Specified by:
getNbUpdatesin interfaceDBTransactionMBean- Returns:
- The number of updates in database.
-
getBadUpdates
public int getBadUpdates()
Returns the number of bad updates in database since last boot (record needing to be inserted)..- Specified by:
getBadUpdatesin interfaceDBTransactionMBean- Returns:
- The number of bad updates in database.
-
getNbDeletes
public final int getNbDeletes()
Returns the number of deletes in database since last boot.- Specified by:
getNbDeletesin interfaceDBTransactionMBean- Returns:
- The number of deletes in database.
-
dbLogExecute
private void dbLogExecute(Hashtable<String,DBOperation> log) throws SQLException
Executes all SQL statements corresponding to log operations.- Parameters:
log- Hashtable containing all operations to commit.- Throws:
SQLException- an error occurs.
-
stop
public void stop()
Stops the transaction module. It waits all transactions termination, then the module is kept in a FREE 'ready to use' state.- Specified by:
stopin interfaceTransaction
-
close
public void close()
Close the transaction module. It waits all transactions termination, the module will be initialized anew before reusing it.- Specified by:
closein interfaceTransaction
-
dumpProperties
public String dumpProperties()
Description copied from interface:DBTransactionMBeanReturns the configuration properties of the component.- Specified by:
dumpPropertiesin interfaceDBTransactionMBean- Returns:
- the configuration properties of the component.
-
dumpProperties
protected void dumpProperties(StringBuilder strbuf)
-
backup
public String backup(String path) throws Exception
Backups the content of Transaction module. Produces a generic Backup/Restore file containing all living objects.- Specified by:
backupin interfaceTransaction- Specified by:
backupin interfaceTransactionMBean- Parameters:
path- Directory path to store the backup.- Returns:
- the name of created backup file.
- Throws:
Exception- An error occurs during backup.
-
-