com.joanju.cg.db
Class DB

java.lang.Object
  extended by com.joanju.cg.db.DB

public class DB
extends java.lang.Object

Core database access.

Most of this class's methods are synchronized for thread-safety.

General access to the database is done through thread-local LocalConnection objects, which are acquired through localConnection().

For the embedded database, we use JDBC directly. If the user has a Hibernate config file, then we use Hibernate and their connection settings.


Field Summary
protected static java.sql.Connection connection
           
protected  java.sql.SQLException connectionException
           
protected static DB instance
           
protected  java.lang.ThreadLocal<com.joanju.cg.db.LocalConnection> localConnections
           
 
Constructor Summary
protected DB()
           
 
Method Summary
protected static void clear()
          Drop and reload the tables.
protected  void dropTables()
           
protected static java.lang.String generateDBName()
           
static java.sql.Connection getThreadLocalConnection()
          Get this thread's Connection TO BE USED FOR READ-ONLY QUERIES ONLY.
protected static DB instance()
          We use a Singleton, because the existance of the instance of this object gives us a promise that the database has been loaded sucessfully, and that it has the schema and startup data loaded.
protected  void loadDatabase()
           
protected static void loadWrapper()
           
static java.sql.Connection newConnection()
          Create a new connection, for reports and such that have to create temp tables.
 java.lang.String schemaString()
          This is only used in my unit tests.
static void shutdown()
           
static void touch()
          Touch the database, just to keep the db connection alive.
protected  void update(java.lang.String expression)
          Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.
protected  void writeMeta()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

connectionException

protected java.sql.SQLException connectionException

connection

protected static java.sql.Connection connection

instance

protected static DB instance

localConnections

protected java.lang.ThreadLocal<com.joanju.cg.db.LocalConnection> localConnections
Constructor Detail

DB

protected DB()
Method Detail

instance

protected static DB instance()
                      throws java.sql.SQLException
We use a Singleton, because the existance of the instance of this object gives us a promise that the database has been loaded sucessfully, and that it has the schema and startup data loaded. If the load fails, then the instance will be null, and an exception will have been thrown.

Returns:
null if the database fails to load.
Throws:
java.io.IOException
java.sql.SQLException

clear

protected static void clear()
                     throws java.sql.SQLException,
                            java.io.IOException
Drop and reload the tables. See Admin.clear(). This should never be public.

Throws:
java.sql.SQLException
java.io.IOException

dropTables

protected void dropTables()
                   throws java.sql.SQLException
Throws:
java.sql.SQLException

generateDBName

protected static java.lang.String generateDBName()

getThreadLocalConnection

public static java.sql.Connection getThreadLocalConnection()
                                                    throws java.sql.SQLException
Get this thread's Connection TO BE USED FOR READ-ONLY QUERIES ONLY. Each thread on the server is given one db connection. This returns it. This is the same connection as is used by table accessors like StringTable. Normally the table accessors should be used instead, but this connection might be used for heavy SQL queries. DO NOT USE THIS WHEN CREATING TEMP TABLES. See newConnection() instead.

Throws:
java.sql.SQLException

loadDatabase

protected void loadDatabase()
                     throws java.sql.SQLException
Throws:
java.sql.SQLException

loadWrapper

protected static void loadWrapper()
                           throws java.sql.SQLException
Throws:
java.sql.SQLException

newConnection

public static java.sql.Connection newConnection()
                                         throws java.sql.SQLException
Create a new connection, for reports and such that have to create temp tables. Usually, the regular table accessors should be used. For read-only SQL access, use getThreadLocalConnection(). Using this for new connections should only be necessary when temp tables need to be created.

Throws:
java.sql.SQLException

schemaString

public java.lang.String schemaString()
                              throws java.sql.SQLException
This is only used in my unit tests.

Throws:
java.sql.SQLException

shutdown

public static void shutdown()
                     throws java.sql.SQLException
Throws:
java.sql.SQLException

touch

public static void touch()
                  throws java.sql.SQLException
Touch the database, just to keep the db connection alive.

Throws:
java.sql.SQLException

update

protected void update(java.lang.String expression)
               throws java.sql.SQLException
Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.

Throws:
java.sql.SQLException

writeMeta

protected void writeMeta()
                  throws java.sql.SQLException
Throws:
java.sql.SQLException


Copyright © 2008 Joanju Software. All Rights Reserved.