org.liquidsite.util.db
Class DatabaseConnection

java.lang.Object
  extended byorg.liquidsite.util.db.DatabaseConnection

public class DatabaseConnection
extends java.lang.Object

A database connection. This class encapsulates a JDBC database connection and holds some additional information needed by the connection pool. When the database connection is no longer needed, it MUST be returned to the database connector so that the used resources can be reused or freed.

See Also:
DatabaseConnector

Method Summary
 void close()
          Closes the connection.
 DatabaseResults execute(DatabaseQuery query)
          Executes a database query or statement.
 void execute(java.io.File file)
          Executes a set of SQL statements from a file.
 java.lang.String getCatalog()
          Returns the current connection catalog.
 int getQueryTimeout()
          Returns the query execution timeout.
 boolean isExpired()
          Checks if this connection has expired.
 boolean isValid()
          Checks if the connection is valid.
 void reset()
          Resets the database connection to default values.
 void setCatalog(java.lang.String catalog)
          Sets the current connection catalog.
 void setQueryTimeout(int timeout)
          Sets the query execution timeout.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isValid

public boolean isValid()
Checks if the connection is valid. A connection is valid until an error is encountered while executing some SQL statement. If the connection is not valid, the connection shouldn't be used.

Returns:
true if the connection is valid, or false otherwise

isExpired

public boolean isExpired()
Checks if this connection has expired. A connection expires when the connection age is more than the timeout.

Returns:
true if the connection has expired, or false otherwise

getQueryTimeout

public int getQueryTimeout()
Returns the query execution timeout. If this value is negative the queries can run without limitation. New connections and connections returned from a connection pool always have a default timeout value.

Returns:
the query execution timeout in seconds, or a negative value for unlimited
See Also:
setQueryTimeout(int), DatabaseConnector.DEFAULT_QUERY_TIMEOUT

setQueryTimeout

public void setQueryTimeout(int timeout)
Sets the query execution timeout. If this value is negative the queries can run without limitation.

Parameters:
timeout - the query execution timeout in seconds, or a negative value for unlimited
See Also:
getQueryTimeout()

getCatalog

public java.lang.String getCatalog()
                            throws DatabaseConnectionException
Returns the current connection catalog.

Returns:
the current connection catalog
Throws:
DatabaseConnectionException - if the database connection couldn't be reestablished

setCatalog

public void setCatalog(java.lang.String catalog)
                throws DatabaseConnectionException,
                       DatabaseException
Sets the current connection catalog.

Parameters:
catalog - the new connection catalog
Throws:
DatabaseConnectionException - if the database connection couldn't be reestablished
DatabaseException - if the database catalog didn't exist

reset

public void reset()
           throws DatabaseConnectionException
Resets the database connection to default values. This will reset the connection to the same state it had when first created. This method is used by the connection pool to guarantee that all connections are returned identical.

Throws:
DatabaseConnectionException - if the database connection couldn't be reestablished

execute

public DatabaseResults execute(DatabaseQuery query)
                        throws DatabaseException
Executes a database query or statement.

Parameters:
query - the database query
Returns:
the database query results, or null for database statements
Throws:
DatabaseException - if the query or statement couldn't be executed correctly

execute

public void execute(java.io.File file)
             throws java.io.FileNotFoundException,
                    java.io.IOException,
                    DatabaseException
Executes a set of SQL statements from a file. Each SQL statement must be terminated by a ';' character.

Parameters:
file - the file with SQL statements
Throws:
java.io.FileNotFoundException - if the file couldn't be found
java.io.IOException - if the file couldn't be read properly
DatabaseException - if some statement couldn't be executed correctly

close

public void close()
Closes the connection.