Re: Use Bean with DbConnectionBroker
Posted By:
Anonymous
Posted On:
Monday, July 23, 2001 08:17 AM
I'm using DBConnectionBroker in a JavaBean. This is the code that I use (and it's working)
========begin JavaBeanCode======
package tmgsitebeans;
import com.javaexchange.dbConnectionBroker.DbConnectionBroker;
import java.io.*;
import java.sql.*;
import java.io.Serializable;
/**
* Description:
* Copyright: Copyright (c) 2001
* @author Razvan T.Bobes
* @version 1.0
*/
public class DBFactory implements java.io.Serializable{
DbConnectionBroker myBroker;
//------------------------------------------------------------------------------
/**
* The DBFactory() bean class constructor;
* Create the Connection Pool
*/
public DBFactory() {
try {
myBroker = new DbConnectionBroker("org.gjt.mm.mysql.Driver",
"jdbc:mysql://192.168.1.11/tmgdatabase",
"tmg","tmg",1,5,
"c:\temp\bm_GlobalPool.log",0.2);
}
catch (IOException e5) { }
}
//------------------------------------------------------------------------------
/**
* Get a connection instance from the Connection Pool
*/
public Connection getConnection(){
Connection conn=null;
int thisConnection=-99;
try {
// Get a DB connection from the Broker
conn = myBroker.getConnection();
thisConnection = myBroker.idOfConnection(conn);
}catch (java.lang.Exception e1) {}
return conn;
}
//------------------------------------------------------------------------------
/**
* Return a Connection instance to the Connection Pool
*/
public void setFreeConnection(Connection myConn) throws java.lang.Exception{
this.myBroker.freeConnection(myConn);
}
//------------------------------------------------------------------------------
/**
* Destroy the Connection Pool
*/
public void setDestroyPool() throws java.lang.Exception{
this.myBroker.destroy();
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
}
========end JavaBeanCode========
from my JSP page I use:
========begin JSP fragment ========
[...]
[...]
//get a connection from the POOL
Connection myConnection=DBFactoryBean.getConnection();
[..]
//return the connection to the POOL
try{
DBFactoryBean.setFreeConnection(myConnection);
}catch(java.lang.Exception e){
out.println("Free connection Error: "+e.toString());
}
[..]
========end JSP fragment ==========
It's hard to read...but copy/paste in
your JavaEditor...it will be clear :)
Hope that helped :)