Use Bean with DbConnectionBroker
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Townsend_Tan
Posted On:   Thursday, May 24, 2001 12:44 AM

Does anybody have the source code of DbConnectionBroker? I tried to make a bean connect to mySQL with javaexchange.dbConnectionBroker, but always throw java.lang.NullPointerException error in "conn = myBroker.getConnection()", what's wrong? it seems that DbConnectionBroker hasn't initialize(). Who knows how to use that? Thanks!

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 :)
About | Sitemap | Contact