• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Passing Database Pool Connection to Java Object

Guest
May 23, 2006 May 23, 2006

Copy link to clipboard

Copied

SO I have a set of Java libs that i would like to use within CFMX. In fact, I would like to do all my business logic with those libraries. Question is, the classes require I pass a database connection to them. How would I do this?
TOPICS
Advanced techniques

Views

1.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2006 May 24, 2006

Copy link to clipboard

Copied

In Coldfusion you can just code the usual Java DB connection directly. That is, something like

<cfscript>
classObj = CreateObject("java", "java.lang.Class");
classObj.forName("etc.etc..Driver");
driverManager = CreateObject("java", "java.sql.DriverManager");
connectionURL="jdbc: etc etc. ";
conn=driverManager.getConnection(connectionURL,username,password);
myQuery="SELECT blah1, blah2 FROM blah3";
preparedStatement = conn.prepareStatement(myQuery);
rs=preparedStatement.executeQuery();
while (rs.next()) {
etc etc
}
rs.close();
conn.close();
</cfscript>

That is for single calls. To set up a permanent connection, you could include in the libraries a class whose job is simply to establish the connection to the DB. I should expect the code to contain some of the themes above, namely,

getConnection (String url, String user, String password)
connectionURL = "jdbc:etc"
java.sql.Connection conn = java.sql.DriverManager.getConnection (connectionURL, username, password);
etc. etc.

You could also find it convenient to store connection and user data data in a properties object of the java.util.Properties class.
java.util.Properties properties = new java.util.Properties ();
properties.setProperty ("user", "bkbk");
properties.setProperty ("password", "knoflook");
java.sql.Connection conn = java.sql.DriverManager.getConnection (connectionURL, properties);






Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 24, 2006 May 24, 2006

Copy link to clipboard

Copied

Thanks for the response.

Anyway I can grab from the CF datasources? I would like to grab from those pools?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 25, 2006 May 25, 2006

Copy link to clipboard

Copied

Anyway I can grab from the CF datasources?
This should be simpler than creating a database connection for your library, which I assumed was your question. When you import a library into a CFML page, e.g. using the cfimport tag, all the Coldfusion datasources are available to it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 26, 2006 May 26, 2006

Copy link to clipboard

Copied

awesome.

How do I pass them to the java object? What var do I use?

Thanks,
-Michael

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2006 May 26, 2006

Copy link to clipboard

Copied

How do I pass them to the java object? What var do I use?
It depends on what your library will be doing and how you would go about the coding. Afterall, you intend to use your libraries to perform some Coldfusion function. If they "expose" their functionality to the CFML page, then you can just use <cfquery name="myQ" datasource="myDSN"> directly. That is, the library will simply know the datasource by its name, in this case, "myDSN".

If not, then I can think of two alternative routes. Extend the library by creating a Java class whose purpose is to establish a database connection. The code is already sketched above. All that the class would need to do is create a java.sql.Connection object to pass to the library, whenever required. Alternatively, define your datasource or cfide.adminapi.administrator object or cfide.adminapi.datasource object or queries, as the case may be, in a Coldfusion component. Then invoke the component in a Java class that you've created specially for that purpose. This so-called CFC Proxy is at present restricted to MX7, Enterprise Edition.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 15, 2007 Feb 15, 2007

Copy link to clipboard

Copied

I stumbled across this Coldfusion TechNote on making a JDBC connection in CFML. It complements the above, and more. In particular, study the download at the bottom.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 15, 2007 Feb 15, 2007

Copy link to clipboard

Copied

I recommend that you use the DBCP library for your JDBC connection pooling - Jrun connection pooling does not work whatsoever.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 15, 2007 Feb 15, 2007

Copy link to clipboard

Copied

> Jrun connection pooling does not work whatsoever.
I'm not sure you're talking about the reference I gave. The technote is about a basic, pure Java JDBC test connection implemented in Coldusion without any connection pooling.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 15, 2007 Feb 15, 2007

Copy link to clipboard

Copied

sure, i wasn't referring to your post.

I was making the point that when michaelprichard tries to implement connection pooling, which he should do for obvious reasons, he should use DBCP, not the built in JRun datasource connection pooling.

I only mention it, because I spent a week trying to figure out why my ap wouldn't perform under load until i read on the serverside that JRun doesn't pool it's connections (even though it says it does)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 16, 2007 Feb 16, 2007

Copy link to clipboard

Copied

LATEST
gotta chime in here. i've been using c3p0
( http://sourceforge.net/projects/c3p0/) in a heavy load java app for over 2
years and haven't had one problem with it. i switched to it from dbcp b/c of
some problems i was having with that package.



"monkey woo too" <webforumsuser@macromedia.com> wrote in message
news:er1k3f$abv$1@forums.macromedia.com...
> sure, i wasn't referring to your post.
>
> I was making the point that when michaelprichard tries to implement
> connection
> pooling, which he should do for obvious reasons, he should use DBCP, not
> the
> built in JRun datasource connection pooling.
>
> I only mention it, because I spent a week trying to figure out why my ap
> wouldn't perform under load until i read on the serverside that JRun
> doesn't
> pool it's connections (even though it says it does)
>
>


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation