getConnectionBeanProvider
of
* ConnectionBeanProvider
must be called, passing the text JNDI
as
* value of the dbDriver
parameter, and the JNDI name of the datasource
* as the value of dbServer
param.
* @author Francesc Busquets (fbusquets@xtec.cat)
* @version 13.09.16
*/
public class JNDIConnectionBeanProvider extends ConnectionBeanProvider{
/**
* Key used for the dbContext
property
*/
public static final String DB_CONTEXT="dbContext";
/**
* String "JNDI", used to indicate ConnectionBeanProvider
that an
* object of this class is required.
*/
public static final String JNDI="JNDI";
/**
* Object used to obtain valid {@link java.sql.Connection} objects.
*/
protected DataSource ds;
/**
* Context where to look for the indicated JNDI resource. In Apache Tomcat, this value should be
* java:comp/env
.
*/
protected String dbContext;
private int totalCBRequests;
private Date started;
private Date lastUse;
private String logFileString;
private PrintWriter log;
private int debugLevel;
private int totalStatements;
private String lastRequest;
/** Creates a new instance of JNDIConnectionBeanProvider */
public JNDIConnectionBeanProvider() {
}
/**
* Main initialization function, called immediatelly after constructor by
* getConnectionBeanProvider functions.
* @param map Collection of key - value pairs that must specify the JNDI Datasource name and
* context params.
* @throws Exception Throwed if the DataSource can't be
* instantiated.
*/
@Override
protected void setUp(Map* Important: You must ever call FreeConnectionBean after the use of the ConnectionBean * object. Typical inmplementation use a try - catch - finally statement block in * order to ensure that all ConnectionBean objects will be properly disposed after * use.
* Example:
*
* ConectionBeanProvider cbp; * java.util.Properties prop=new Java.util.Properties(); * // ... * // ... fill-up properties with values for dbDriver, dbServer, dbLogin, etc. * // ... * cbp=ConnectionBeanProvider.getConnectionBeanProvider(map); * ConnectionBean cb=cbp.getConnectionBean(); * try { * // ... use of the ConnectionBean object * } catch(Exception ex){ * // ... process possible exceptions done while database access * } finally { * // Very important: free the ConnectionBean object: * cbp.freeConnectionBean(cb); * } ** @return A ready-to-use ConnectionBean. Remember to return it by calling * FreeConnectionBean. */ public ConnectionBean getConnectionBean() { ConnectionBean result=null; try{ if(ds!=null){ Connection con=ds.getConnection(); result=new ConnectionBean(con, mapStatements); totalCBRequests++; lastUse=new Date(); } if(log!=null && debugLevel>2) log.println(new Date().toString()+" - connection request"); } catch(Exception ex){ if(log!=null) log.println(new Date().toString() + " Unable to get DB connection: "+ex.getMessage()); } return result; } }