Package lazyj
Class DBFunctions.DBConnection
java.lang.Object
lazyj.DBFunctions.DBConnection
- Enclosing class:
- DBFunctions
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:
- 
Method SummaryModifier and TypeMethodDescriptionbooleancanUse(long checkInterval, boolean validateUsingQuery) Find out if this connection is free to usevoidclose()Really close a connection to the databaseprotected voidfinalize()On object deallocation make sure that the connection is properly closed.booleanfree()Mark a previously used connection as free to be used by somebody elseGet the established JDBC connection for direct access to the database.Get the current descriptionbooleanCheck the (cached) status of this connection.voidsetDescription(String description) Set the description to an arbitrary string to be used when debugging a problem.booleansetReadOnly(boolean newValue) Set the read-only connection flag.booleanuse()Use this connection, by marking it as busy and setting the last access time to the current time.
- 
Method Details- 
getConnectionGet the established JDBC connection for direct access to the database.- Returns:
- the JDBC connection
 
- 
canUsepublic 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 checked
- validateUsingQuery- if- truethen execute some explicit query
- Returns:
- true if free, false if busy or other error
 
- 
usepublic 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
 
- 
freepublic 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
 
- 
closepublic void close()Really close a connection to the database
- 
finalizeprotected void finalize()On object deallocation make sure that the connection is properly closed.
- 
setDescriptionSet the description to an arbitrary string to be used when debugging a problem.- Parameters:
- description- the description to set
 
- 
getDescriptionGet the current description- Returns:
- the description
 
- 
isReadOnlypublic 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
 
- 
setReadOnlypublic 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
 
 
-