Package lazyj
Class DBFunctions.DBConnection
java.lang.Object
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:
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(ExtProperties)
-
Method Summary
Modifier and Type Method Description boolean
canUse(long checkInterval, boolean validateUsingQuery)
Find out if this connection is free to usevoid
close()
Really close a connection to the databaseprotected 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 elsejava.sql.Connection
getConnection()
Get the established JDBC connection for direct access to the database.java.lang.String
getDescription()
Get the current descriptionboolean
isReadOnly()
Check the (cached) status of this connection.void
setDescription(java.lang.String description)
Set the description to an arbitrary string to be used when debugging a problem.boolean
setReadOnly(boolean newValue)
Set the read-only connection flag.boolean
use()
Use this connection, by marking it as busy and setting the last access time to the current time.
-
Method Details
-
getConnection
public java.sql.Connection getConnection()Get the established JDBC connection for direct access to the database.- Returns:
- the JDBC connection
-
canUse
public boolean canUse(long checkInterval, boolean validateUsingQuery)Find out if this connection is free to use- Parameters:
checkInterval
- if strictly positive then how often (in milliseconds) to check if the connection is still valid, otherwise it will always be checkedvalidateUsingQuery
- iftrue
then execute some explicit query- Returns:
- true if free, false if busy or other error
-
use
public 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 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 void close()Really close a connection to the database -
finalize
protected void finalize()On object deallocation make sure that the connection is properly closed.- Overrides:
finalize
in classjava.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
-
isReadOnly
public boolean isReadOnly()Check the (cached) status of this connection. Doesn't go to the driver to ask for it but instead this is the last requested state.- Returns:
- the read-only status of this connection
-
setReadOnly
public boolean setReadOnly(boolean newValue)Set the read-only connection flag. Only goes to the driver with the request in case the flag is different from the previously known value.- Parameters:
newValue
-- Returns:
- previous value of the read only flag
-