org.aselect.system.db
Class SQLDatabaseConnector

java.lang.Object
  extended by org.aselect.system.db.SQLDatabaseConnector

public class SQLDatabaseConnector
extends java.lang.Object

SQL database connection functionality.

Description: br> Database connection and query functionality for an SQL database (e.g. MySQL).

If you are using transactions safe tables (like InnoDB, BDB) you can use the startTransaction(Statement),commitTransaction(Statement), and rollbackTransaction(Statement)methods to start, commit and rollback transactions.

Concurrency issues:
The functionality of this class is threadsafe. All critical methods are synchronized.

Author:
Alfa & Ariss

Constructor Summary
SQLDatabaseConnector(java.lang.String sDriverName, java.lang.String sUser, java.lang.String sPassword, java.lang.String sURL, SystemLogger systemLogger)
          Create a new instance.
 
Method Summary
 void commitTransaction(java.sql.Statement oStmt)
          Excute the "COMMIT" command.
 java.sql.Statement connect()
          Connect to the database.
 boolean disconnect(java.sql.Statement oStmt)
          Close the database connection.
 java.sql.ResultSet executeQuery(java.sql.Statement oStmt, java.lang.String sQuery)
          Execute a SQL query.
 int executeUpdate(java.sql.Statement oStmt, java.lang.String sQuery)
          Execute a SQL update query.
 java.lang.String getDatabaseUrl()
          Returns the database URL.
 void rollbackTransaction(java.sql.Statement oStmt)
          Excute the "ROLLBACK" command.
 void startTransaction(java.sql.Statement oStmt)
          Execute the "BEGIN" command.
 void updateCount(boolean bConnect)
          Update connection counter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SQLDatabaseConnector

public SQLDatabaseConnector(java.lang.String sDriverName,
                            java.lang.String sUser,
                            java.lang.String sPassword,
                            java.lang.String sURL,
                            SystemLogger systemLogger)
                     throws ASelectDatabaseException
Create a new instance.

Description:
Create a new SQLDatabaseConnector:
Concurrency issues:
-

Preconditions:
Postconditions:
The systemlogger and database variables have been set.

Parameters:
sDriverName - The database driver name.
sUser - The database user.
sPassword - The database password.
sURL - The database URL.
systemLogger - The system logger.
Throws:
ASelectDatabaseException - If database driver initialization fails.
Method Detail

updateCount

public void updateCount(boolean bConnect)
Update connection counter.

Description:
Updates the number of currently open connections.

Concurrency issues:
This method is synchronized.

Preconditions:
-

Postconditions:
The number of connections is updated.

Parameters:
bConnect - If true the number of open connections is increased, otherwise it is decreased.

connect

public java.sql.Statement connect()
Connect to the database.

Description:
This methods performs the following steps:
Concurrency issues:
This method is synchronized.

Preconditions:
-

Postconditions:
There is a connection with the database.

Returns:
A new created Statement.

disconnect

public boolean disconnect(java.sql.Statement oStmt)
Close the database connection.

Description:
Close the given database statement and if applicable the database connection.

This methods performs the following steps:
Concurrency issues:
This method is synchronized.

Preconditions:
oStmt should be retrieved by calling connect()

Postconditions:
The connection counter is lowered. If the connection counter is 0, the database connection is closed.

Parameters:
oStmt - The statement that is to be closed.
Returns:
True if closing succeeded, otherwise false.

executeQuery

public java.sql.ResultSet executeQuery(java.sql.Statement oStmt,
                                       java.lang.String sQuery)
                                throws ASelectDatabaseException
Execute a SQL query.

Description:
Uses the given Statement to execute the given query.

Concurrency issues:
Every thread should use its own statement. This method should be called sequentially when using the same Statement.

Preconditions:
Postconditions:
-

Parameters:
oStmt - The statement that is used for rhe execution of the query.
sQuery - The query that is to be executed.
Returns:
The result of the query.
Throws:
ASelectDatabaseException - If execution fails.

executeUpdate

public int executeUpdate(java.sql.Statement oStmt,
                         java.lang.String sQuery)
                  throws ASelectDatabaseException
Execute a SQL update query.

Description:
Uses the given Statement to execute the given query. The query updates data in the database.

Concurrency issues:
Every thread should use its own statement. This method should be called sequentially when using the same Statement.

Preconditions:
Postconditions:
-

Parameters:
oStmt - The statement that is used for the execution of the query.
sQuery - The query that is to be executed.
Returns:
The number of updated rows.
Throws:
ASelectDatabaseException - If update fails.
See Also:
Statement.executeUpdate(java.lang.String)

startTransaction

public void startTransaction(java.sql.Statement oStmt)
                      throws ASelectDatabaseException
Execute the "BEGIN" command.

Description:
Excute the SQL "BEGIN" command as a query. This command marks the beginning of a transaction. This is the recommended way to start an ad-hoc transaction as this is SQL-99 syntax.

Concurrency issues:
Every thread should use its own statement.

Preconditions:
oStmt should be retrieved by calling connect()

Postconditions:
The database has started a new transaction.

Parameters:
oStmt - The statement that is used for the execution.
Throws:
ASelectDatabaseException - If transaction could not be started.

rollbackTransaction

public void rollbackTransaction(java.sql.Statement oStmt)
                         throws ASelectDatabaseException
Excute the "ROLLBACK" command.

Description:
Excute the SQL "ROLLBACK" command as a query. Use this command if you want to ignore the changes you have made since the beginning of your transaction.

Concurrency issues:
Every thread should use its own statement.

Preconditions:
Postconditions:
All queries during transaction are rolled back.

Parameters:
oStmt - The statement that is used for the execution.
Throws:
ASelectDatabaseException - If transaction could not be rolled back.

commitTransaction

public void commitTransaction(java.sql.Statement oStmt)
                       throws ASelectDatabaseException
Excute the "COMMIT" command.

Description:
Excute the SQL "COMMIT" command as a query. After the "COMMIT" query the recent changes are stored.

Concurrency issues:
Every thread should use its own statement.

Preconditions:
Postconditions:
All queries during transaction are committed.

Parameters:
oStmt - The statement that is used for the execution.
Throws:
ASelectDatabaseException - If transaction could not be committed.

getDatabaseUrl

public java.lang.String getDatabaseUrl()
Returns the database URL.

Returns:
The URL of the database.


Copyright © 2008 SURFnet BV. All Rights Reserved.