
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()) |
|
|
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.
Definition at line 53 of file DB.java.
00054 {
00055 this(new DBPool(config));
00056 }
|
|
|
Return a DB borrowed from the provided pool of DB connections. Use this for mainstream DB construction.
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 }
|
|
|
Return this DB to the DB pool. The transaction is neither committed nor rolledback.
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 }
|
|
|
Commit this transaction.
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 }
|
|
|
Execute the provided SQL string. This is for SQL statements that don't return ResultSets.
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 }
|
|
|
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.
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 }
|
|
|
Lock the provided list of dbms tables
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 }
|
|
|
Construct a DBQuery instance. These have roughly the functionality of JDBC ResultSets.
Definition at line 158 of file DB.java.
00160 {
00161 return new DBQuery(this, sql);
00162 }
|
|
|
Rollback (cancel) this transaction.
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 }
|
|
|
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 }
|
|
|
Construct a DBUpdate instance. These have roughly the functionality of JDBC PreparedStatements.
Definition at line 171 of file DB.java.
00173 {
00174 return new DBUpdate(this, sql);
00175 }
|