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

DB.java

00001 package edu.virtualschool.jwaa.dbms;
00002 
00003 import java.io.File;
00004 import java.sql.Connection;
00005 import java.sql.SQLException;
00006 import java.sql.Statement;
00007 
00008 import org.apache.log4j.Logger;
00009 
00010 import edu.virtualschool.jwaa.Config;
00011 
00028 public  final class DB 
00029 {
00030   private final DBPool pool;
00031   public Connection connection;
00032   
00033   final Logger logger = Logger.getLogger(DB.class.getName());
00034   
00035   public DB(File file) throws Config.Fault, DBPool.Fault, DB.Fault
00036   {
00037     this(new Config(file));
00038   }
00039   public DB(String path) throws Config.Fault, DBPool.Fault, DB.Fault
00040   {
00041     this(new Config(path));
00042   }
00053   public DB(Config config) throws Config.Fault, DBPool.Fault, DB.Fault
00054   {
00055     this(new DBPool(config));
00056   }
00064   public DB(DBPool pool)  throws DBPool.Fault, DB.Fault
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   }
00089   public  final void commit() throws DB.Fault
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   }
00106   public  final void rollback() throws DB.Fault
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   }
00124   public  final void close() throws DB.Fault
00125   {
00126     if (connection != null)
00127     {
00128       pool.checkin(connection);
00129       connection = null;
00130     }
00131   }
00139   public final void finalize()
00140   {
00141     try
00142     {
00143       close();
00144     }
00145     catch (Throwable e)
00146     {
00147       logger.error(e, e);
00148     }
00149   }
00158   public  final DBQuery query(String sql)
00159     throws DB.Fault
00160   {
00161     return new DBQuery(this, sql);
00162   }
00171   public  final DBUpdate update(String sql)
00172     throws DB.Fault
00173   {
00174     return new DBUpdate(this, sql);
00175   }
00176   
00183   public  final void execute(String sql)
00184     throws DB.Fault
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   }
00202   public final void lock(Object[] tables) throws Fault
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   }
00212   public final void unlock() throws Fault
00213   {
00214     execute("unlock tables");
00215   }
00221   public final static class Fault extends edu.virtualschool.jwaa.Fault
00222   {
00223     public Fault(Object m, Throwable e) { super(m, e); }
00224     public Fault(Object m) { super(m, null); }
00225     public Fault(Throwable e) { super(e, e); }
00226   }
00227 }