lazyj
Class DBFunctions.DBConnection

java.lang.Object
  extended by lazyj.DBFunctions.DBConnection
Enclosing class:
DBFunctions

public static final class DBFunctions.DBConnection
extends java.lang.Object

Wrapper around a raw database connection. You cannot create this object directly and you must free the connections properly otherwise you will run in big trouble.

Here is a sample code:

 // set the connection parameters 
ExtProperties dbProp = new ExtProperties(); dbProp.set("driver", "org.postgresql.Driver"); // mandatory dbProp.set("database", "somedb"); // mandatory dbProp.set("host", "127.0.0.1"); // defaults to 127.0.0.1 if missing dbProp.set("port", "5432"); // DB-dependend default if missing dbProp.set("user", "username"); // recommended dbProp.set("password", "*****"); // recommended // you can also set here various other configuration options that the JDBC driver will look at DBFunctions db = new DBFunctions(dbProp); DBFunctions.DBConnection conn = db.getConnection(); if (conn==null) return; Statement stat = null; ResultSet rs = null; try{ stat = conn.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); stat.execute(someQuery, Statement.NO_GENERATED_KEYS); rs = stat.getResultSet(); // .................. rs.close(); rs = null; stat.close(); stat = null; } catch (Exception e){ ... } finally{ if (rs!=null){ // close try{ rs.close(); } catch (Exception e){ } } if (stat!=null){ try{ stat.close(); } catch (Exception e){ } } conn.free(); }

Since:
Jan 17, 2009
Author:
costing
See Also:
DBFunctions.DBFunctions(ExtProperties)

Method Summary
 boolean canUse()
          Find out if this connection is free to use
 void close()
          Really close a connection to the database
protected  void finalize()
          On object deallocation make sure that the connection is properly closed.
 boolean free()
          Mark a previously used connection as free to be used by somebody else
 java.sql.Connection getConnection()
          Get the established JDBC connection for direct access to the database.
 java.lang.String getDescription()
          Get the current description
 void setDescription(java.lang.String description)
          Set the description to an arbitrary string to be used when debugging a problem.
 boolean use()
          Use this connection, by marking it as busy and setting the last access time to the current time.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getConnection

public final java.sql.Connection getConnection()
Get the established JDBC connection for direct access to the database.

Returns:
the JDBC connection

canUse

public final boolean canUse()
Find out if this connection is free to use

Returns:
true if free, false if busy or other error

use

public final boolean use()
Use this connection, by marking it as busy and setting the last access time to the current time.

Returns:
true if the connection was free and could be used, false if it was not available

free

public final boolean free()
Mark a previously used connection as free to be used by somebody else

Returns:
true if the connection was in use and was freed, false if the connection was in other state

close

public final void close()
Really close a connection to the database


finalize

protected final void finalize()
On object deallocation make sure that the connection is properly closed.

Overrides:
finalize in class java.lang.Object

setDescription

public void setDescription(java.lang.String description)
Set the description to an arbitrary string to be used when debugging a problem.

Parameters:
description - the description to set

getDescription

public java.lang.String getDescription()
Get the current description

Returns:
the description