I would use datasources to solve your problem. The idea is that you
configure a datasource in JRun using the management console, (see your
documentation). You specify the url of the database server, the type of
database, the username and password, etc. etc. If your database is not
one of the default types that JRun supports then you will need to
provide a .jar file with the JDBC driver and put it on JRun's classpath.
After you configure the datasource, you can test it to see if JRun can
connect properly.
After you have done this you change the code in your application to get
connections from the datasource instead of manually creating them. To
do this you must do a JNDI lookup to retrieve the datasource object.
See my sample code below. You use the connection just like normal and
when you are done with it, you close it.
The advantage of using a datasource is that it creates a pool of
database connections. When you ask for a connection it gives you an
established connection from the pool. When you close the connection, it
doesn't really close the connection, it simply returns it to the pool.
When you do it this way your application will tend to generate a small
pool of connections which will stay open but the number will be more
constant.
Another advantage of using datasources is that the information about the
database and database server is stored outside of your code and can be
changed at any time without recompiling your application.
Here is a method that retrieves a datasource and stores it in a static
member variable within a class that we use to produce connections.
private synchronized static void getDataSource()
{
try
{
System.setSecurityManager( new RMISecurityManager() );
//m_ctx is a static variable of type: InitialContext
m_ctx = new InitialContext( );
//m_ds is a static member variable of type: DataSource
//"BYUDSSData" is the datasource name as configured in the
JRun management console.
m_ds = (DataSource)m_ctx.lookup(
"java:comp/env/jdbc/BYUDSSData" );
if( m_ds == null )
{
Logger.log("Failed to retrieve DataSource!", null,
Logger.CRITICAL_ERROR );
}
else
{
Logger.log("DataSource successfully retrieved!", null,
Logger.DEBUG_MSG );
}
}
catch( Exception e )
{
Logger.log("Failed to retrieve DataSource!", e,
Logger.CRITICAL_ERROR );
}
}
Here is a static method which returns a database connection which is
produced by the datasource that we retrieved with the previous method.
public static Connection getDBConnection()
{
//The connection which we will return.
Connection ReturnValue = null;
try
{
//If we haven't already retrieved the DataSource, go and do
it now.
if(( m_ctx == null ) || ( m_ds == null ))
{
getDataSource();
}
//If we were successful in getting the DataSource, use it to get
//a Connection.
if( m_ds != null )
{
ReturnValue = m_ds.getConnection();
}
}
catch( Exception e )
{
Logger.log( "Error retrieving a database connection.", e,
Logger.DATABASE_ERROR );
}
return ReturnValue;
}
I hope this helps.
[EMAIL PROTECTED] wrote:
>Our office is new in implementing J2E technology. We are set up with JRun and ms
>sql server. The db admin was horrifed when he opened the sql server log and saw
>all the connections by one of my applications.
>
>
>Basically, the site queries the db almost at every page, to collect, display the
>different info. So everytime the user browses a page, data is being pulled from
>sql.
>
>
>Am I doing anything wrong? I thought that everytime I connect, I should close
>the connection.
>
>
>I posted this q to javaranch, and one person replied that i should store in app
>level, and another one that JDBC2 Datasource would do that for me.
>
>
>I'm embarrassed to admit but am clueless of what they mean. Can someone please
>explain?
>
>
>Thanks!
>
______________________________________________________________________
This list and all House of Fusion resources hosted by CFHosting.com. The place for
dependable ColdFusion Hosting.
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists