Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

edu.virtualschool.jwaa.dbms.DB Class Reference

Collaboration diagram for edu.virtualschool.jwaa.dbms.DB:

Collaboration graph
[legend]
List of all members.

Detailed Description

Author:
bcox
Each instance retrieves a database connection from a DB pool, and returns it to the pool when the close() or finalize() methods are called.

Transactional updates are supported via commit() and rollback() methods.

Instances provide methods for initiating database queries and updates by returning DBQuery or DBUpdate instances respectively. DBQuery instances support JDBC ResultSet functionality and DBUpdate instances support JDBC PreparedStatement functionality.

Definition at line 28 of file DB.java.

Public Member Functions

 DB (File file) throws Config.Fault, DBPool.Fault, DB.Fault
 DB (String path) throws Config.Fault, DBPool.Fault, DB.Fault
 DB (Config config) throws Config.Fault, DBPool.Fault, DB.Fault
 DB (DBPool pool) throws DBPool.Fault, DB.Fault
final void commit () throws DB.Fault
final void rollback () throws DB.Fault
final void close () throws DB.Fault
final void finalize ()
final DBQuery query (String sql) throws DB.Fault
final DBUpdate update (String sql) throws DB.Fault
final void execute (String sql) throws DB.Fault
final void lock (Object[] tables) throws Fault
final void unlock () throws Fault

Public Attributes

Connection connection

Package Attributes

final Logger logger = Logger.getLogger(DB.class.getName())


Constructor & Destructor Documentation

edu.virtualschool.jwaa.dbms.DB.DB Config  config  )  throws Config.Fault, DBPool.Fault, DB.Fault
 

Construct a single DB from the provided JDBC configuration. This is primarily useful for single-use connections. It is normally more efficient to use DBPool(config) to create a connection pool and pass that to new DB(pool) for each connection.

Parameters:
config: the configuration
Exceptions:
Config.Fault 
DBPool.Fault 
DB.Fault 

Definition at line 53 of file DB.java.

00054   {
00055     this(new DBPool(config));
00056   }

edu.virtualschool.jwaa.dbms.DB.DB DBPool  pool  )  throws DBPool.Fault, DB.Fault
 

Return a DB borrowed from the provided pool of DB connections. Use this for mainstream DB construction.

Parameters:
pool: the connection pool
Exceptions:
DBPool.Fault 
DB.Fault 

Definition at line 64 of file DB.java.

References edu.virtualschool.jwaa.dbms.DBPool.checkout().

00065   {
00066     this.pool = pool;
00067     try
00068     {
00069       this.connection = pool.checkout();
00070     }
00071     catch (DBPool.Fault e)
00072     {
00073       logger.error("new DB(pool) failed, retrying...", e);
00074       try
00075       {
00076         this.connection = pool.checkout();
00077       }
00078       catch (DBPool.Fault e1)
00079       {
00080         logger.error("new DB(pool) retry failed, giving up", e1);
00081         throw e;
00082       }
00083     }
00084   }


Member Function Documentation

final void edu.virtualschool.jwaa.dbms.DB.close  )  throws DB.Fault
 

Return this DB to the DB pool. The transaction is neither committed nor rolledback.

Exceptions:
DB.Fault 

Definition at line 124 of file DB.java.

References edu.virtualschool.jwaa.dbms.DBPool.checkin().

Referenced by edu.virtualschool.jwaa.dbms.DB.finalize(), and edu.virtualschool.jwaa.xml.Controller.run().

00125   {
00126     if (connection != null)
00127     {
00128       pool.checkin(connection);
00129       connection = null;
00130     }
00131   }

final void edu.virtualschool.jwaa.dbms.DB.commit  )  throws DB.Fault
 

Commit this transaction.

Exceptions:
DB.Fault 

Definition at line 89 of file DB.java.

Referenced by edu.virtualschool.jwaa.xml.Controller.run().

00090   {
00091     if (connection == null)
00092       throw new DB.Fault("commit to a null connection");
00093     try
00094     {
00095       connection.commit();
00096     }
00097     catch (SQLException e)
00098     {
00099       throw new Fault(e, e);
00100     }
00101   }

final void edu.virtualschool.jwaa.dbms.DB.execute String  sql  )  throws DB.Fault
 

Execute the provided SQL string. This is for SQL statements that don't return ResultSets.

Parameters:
sql 
Exceptions:
DB.Fault 

Definition at line 183 of file DB.java.

Referenced by edu.virtualschool.jwaa.dbms.DB.lock(), and edu.virtualschool.jwaa.dbms.DB.unlock().

00185   {
00186     try
00187     {
00188       Statement stmt = connection.createStatement();
00189       stmt.execute(sql);
00190       stmt.close();
00191     }
00192     catch (SQLException e)
00193     {
00194       throw new Fault(sql, e);
00195     }
00196   }

final void edu.virtualschool.jwaa.dbms.DB.finalize  ) 
 

Finalize this DB by closing it automatically when the instance is garbage collected. Don't rely on this if performance is an issue. Call the close method explicitly to minimize overutilization of database connections.

See also:
java.lang.Object#finalize()

Definition at line 139 of file DB.java.

References edu.virtualschool.jwaa.dbms.DB.close().

00140   {
00141     try
00142     {
00143       close();
00144     }
00145     catch (Throwable e)
00146     {
00147       logger.error(e, e);
00148     }
00149   }

final void edu.virtualschool.jwaa.dbms.DB.lock Object[]  tables  )  throws Fault
 

Lock the provided list of dbms tables

Parameters:
tables: The tableNames to be locked

Definition at line 202 of file DB.java.

References edu.virtualschool.jwaa.dbms.DB.execute().

00203   {
00204     final StringBuffer sql = new StringBuffer("lock tables ");
00205     for (int i = 0; i < tables.length; i++)
00206       sql.append(tables[i] + " write" + (i < tables.length - 1 ? ", " : ""));
00207     execute(sql.toString());
00208   }

final DBQuery edu.virtualschool.jwaa.dbms.DB.query String  sql  )  throws DB.Fault
 

Construct a DBQuery instance. These have roughly the functionality of JDBC ResultSets.

Parameters:
sql 
Returns:
Exceptions:
DB.Fault @ see ResultSet

Definition at line 158 of file DB.java.

00160   {
00161     return new DBQuery(this, sql);
00162   }

final void edu.virtualschool.jwaa.dbms.DB.rollback  )  throws DB.Fault
 

Rollback (cancel) this transaction.

Exceptions:
DB.Fault 

Definition at line 106 of file DB.java.

Referenced by edu.virtualschool.jwaa.xml.Controller.run().

00107   {
00108     if (connection == null)
00109       throw new DB.Fault("rollback to a null connection");
00110     try
00111     {
00112       connection.rollback();
00113     }
00114     catch (SQLException e)
00115     {
00116       throw new DB.Fault("rollback failed", e);
00117     }
00118   }

final void edu.virtualschool.jwaa.dbms.DB.unlock  )  throws Fault
 

Release the lock acquired above.

Definition at line 212 of file DB.java.

References edu.virtualschool.jwaa.dbms.DB.execute().

00213   {
00214     execute("unlock tables");
00215   }

final DBUpdate edu.virtualschool.jwaa.dbms.DB.update String  sql  )  throws DB.Fault
 

Construct a DBUpdate instance. These have roughly the functionality of JDBC PreparedStatements.

Parameters:
sql 
Returns:
Exceptions:
DB.Fault 
See also:
PreparedStatement

Definition at line 171 of file DB.java.

00173   {
00174     return new DBUpdate(this, sql);
00175   }


The documentation for this class was generated from the following file: