Networked In-Memory Databases With ColdFusion

Posted By : todd sharp Posted At : January 28, 2011 11:31 AM Posted In: ColdFusion

12

Yesterday I posted about creating an in-memory database with ColdFusion and Apache Derby. It was a pretty cool proof of concept, but in practice it is a bit limited because it was using the Apache Derby Embedded database which limits you to a single JVM connection at a time (meaning not even a GUI interface can connect once CF grabs ahold of it). In simple applications this might not be an issue, but I wanted to see if I could get things working with the Network Server version of Derby.

Turns out it's just as easy, and in fact it can get a whole lot easier to work with by creating a ColdFusion DSN for the in-memory database. Having a proper DSN will let us use <cfquery> and <cfqueryparam> and work with the in-memory DB as we would any standard file system DB. The first step is to set up a DSN. Choose a name for the DSN and select 'other' as your driver type. On the next screen you'll simply craft the connection string and enter the proper class names as shown below:

You'll notice that ColdFusion will be unable to verify the DSN when you save it. That's because the Derby database isn't running yet.

You can easily start the database from the command line (see the Derby docs for the NetworkServiceControl), but wouldn't it be much easier to handle it programatically via code? Of course it would! Here's how you'd do that (imagine this code residing in onApplicationStart):

<cfscript>
ip = [127,0,0,1];
ba = javaCast('byte[]', ip);

inetAddress = createObject('java', 'java.net.Inet4Address').getByAddress(ba);

nsc = createObject('java', 'org.apache.derby.drda.NetworkServerControl').init(inetAddress, '1527');

writeDump(nsc);

try{
    writeDump(nsc.ping());    
}
catch(Any e){
    //ping unsuccessful - start the server
    nsc.start(javacast('null', ''));    
}

writeDump(nsc.getCurrentProperties());

//uncomment to shutdown
//nsc.shutdown();
</cfscript>

And now you can do things like this:

<cftry>
    <cfquery name="create" datasource="memory_network_server">
    CREATE TABLE testTable (
     testID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
     test VARCHAR(50)
    )
    </cfquery>
<cfcatch type="any">
    <!--- table exists --->
</cfcatch>
</cftry>

<cfquery name="insert" datasource="memory_network_server">
insert into testTable (test)
values(<cfqueryparam value="foo" cfsqltype="cf_sql_varchar" />)
</cfquery>

<cfquery name="select" datasource="memory_network_server">
select *
from testTable
</cfquery>

<cfdump var="#select#">

I haven't tested it yet, but in theory any other application in a cluster should be able to create a DSN pointing at the server that launches the database and interact with it just as you would a standard database. You should theoretically also be able to connect up with an IDE or GUI query management tool to make management and table creation even easier (that's my next test).

I should mention that in-memory databases will only work with ColdFusion 9.0.1 because the version of Apache Derby that supports in-memory databases didn't ship with ColdFusion until that updater. I may take a look at using JavaLoader to make things work with older versions of CF at some point in the future.

Comments (12)

todd sharp's Gravatar I should mention that you might want to set logConnections to false when you're starting the server:

nsc.logConnections(false);

Otherwise Derby will make a log entry for every single hit to the DB which could quickly fill up your log files.

Chris Blackwell's Gravatar This is great. I just tried this out in combination with CF ORM, setting the dialect to derby and dsn to my in-memory database and it works a treat.

Should you call nsc.logConnections(false) before or after you start the server ?

todd sharp's Gravatar I'm not sure it matters if you call it before or after. You can always check the log files and see if it works...

Glad to see it works with ORM - I was gonna try that myself :) Probably a really nice way to just hit the ground running with an ORM app!

Aaron Greenlee's Gravatar Yesterday's version did not work on a server with Fusion Reactor. Today's is just perfect!

THANKS!

coach outet online's Gravatar Welcome to my site , there has a lot of what you want, open it there could be a surprise

waiting for you!

http://www.onlinecoachfactoryoutlets.com coach outlet
http://www.onlinecoachfactoryoutlets.com coach outlet online
http://www.onlinecoachfactoryoutlets.com coach factory outlet
http://www.onlinecoachfactoryoutlets.com coach outlet store

O(∩_∩)O~

torrent's Gravatar Great.I just tried this out in combination with CF ORM, setting the dialect to derby and dsn to my in-memory database and it works a treat.

ralphlauren outlet's Gravatar hello,I Like it very much!垃圾你妹啊``

sooojerseys's Gravatar http://www.cowboysfansjersey.com Cowboys Jerseys
http://www.jetsfansshop.com New York Jets Jerseys

bears jersey's Gravatar Considerably, the article is in reality the greatest on this noteworthy topic. I agree with your conclusions and will eagerly look forward to your next updates. Saying thanks will not just be sufficient , for the wonderful clarity in your writing. I will immediately grab your rss feed to stay privy of any updates. Pleasant work and much success
http://www.bearsprostore.com
http://www.yankeesmlbjerseys.com

Vicky's Gravatar nice post dear.. :)

http://www.birthdaybless.com/ birthday party organisers delhi

http://www.prestigepackersmovers.com/ professional packers and movers

http://www.maxfortrelocation.com/movers-packers-pu... Movers and packers pune