Mark Drew (Redux)- cf_etc...

a compendium of railo, cfml, cfeclipse and technology topics

Mark Drew (Redux)- cf_etc...

Fun with mappings and resources: Part 3 DB Resources

April 14, 2009 ·

In my last post I wrote about using RAM as a nice resource to render things from. For this post I shall go through doing a quick demo of the database resource.

That's right. You can use a database exacly like you would a file system.

Before we get started, with the latest version of Railo you need to first add the following line into your <web-root>/WEB-INF/railo/railo-web.xml.cfm

 

<resources> ...
<resource-provider arguments="case-sensitive:true;lock-timeout:1001;prefix:test_;" class="railo.commons.io.res.type.datasource.DatasourceResourceProvider" scheme="db"/>
</resources>

It might be there already but commented out. This essentially adds another resource that you can use to create mappings.

Next, just create a database in mysql, nothing fancy, you just need to then create a datasource in either your web or server admin. I called mine "resourcedb" (imaginative eh?)

Now that we have the database, we need to create a mapping to this database. To address this newly created datasource as a mapping we add the following under the Archives & Resources - Mappings section.

Virtual: /dbresource
Resource: db://resourcedb

Now you are set! Here is my code now:

<cfif NOT DirectoryExists("/dbresource/elvis/")>
<cfdirectory action="create" directory="/dbresource/elvis"/>
</cfif>
<cffile action="write" file="/dbresource/elvis/dump.cfm" output="<cfdump eval=server>">
<cfdirectory action="list" directory="/dbresource" name="dbfiles" recurse="true"/>
<cfdump eval=dbfiles />

And you should now get a nice list of the files in your db resource. This is useful if you want to share these files across distributed servers without exposing the file system itself.

 

Tags: getrailo · railo

6 responses

  • 1 Andy Sandefer // Apr 21, 2009 at 10:34 PM

    @Mark,
    Sorry for being so stupid on this but can you explain a bit more as to how this works? Are you storing files in the database in a table named elvis...
    <cfif NOT DirectoryExists("/dbresource/elvis/")>

    If so did you not have to define the DDL somewhere for the schema?

    This looks interesting I'm just a bit lost as to how it works.

    Thanks,
    Andy
  • 2 Ms Willis // Apr 22, 2009 at 6:37 AM

    It amazes me that people find fun in this stuff! Me i like cooking and Oprah!
  • 3 Andy Sandefer // Apr 22, 2009 at 8:31 PM

    @Ms Willis
    Well now that I've seen your blog I can safely say that you and I have at least 3 things in common...

    We both like

    Young Jeezy
    50 Cent
    Lil Jon

    Make that 4 things (I like Oprah too but I'm usually stuck in the office coding when she's on).

    Cheers to Ms Willis!
  • 4 Mark Drew // Apr 27, 2009 at 9:20 PM

    @Andy: The database tables will be created for you, you don't need to do anything! :)

    @Ms Willis: I find lots of things fun, but most of them I wouldn't publish on this blog... ;)
  • 5 jason // May 20, 2009 at 12:09 PM

    Followed your instructions but getting error when I restart Railo. Instead of using your example names, I used my own. Here's what the line looks like in "railo-web.xml.cfm"

    <mapping physical="db://dbfilesystem" primary="physical" toplevel="true" trusted="false" virtual="/dbfs"/>

    I created a Database Resource called "dbfilesystem" and an abbreviated pathname "/dbfs". Using your example CF code, I was able to create a "test" directory -- I looked in the Database and see the entry.

    So, it seems to work. However, when I restart Railo, I get the following error when trying to access my web context (where the virtual resource was added.)

    HTTP ERROR: 500

    datasource [dbfilesystem] doesn't exist
    RequestURI=/railo-context/admin/server.cfm

    Caused by:

    javax.servlet.ServletException: datasource [dbfilesystem] doesn't exist
       at railo.commons.io.res.type.datasource.DatasourceResourceProvider.getAttr(DatasourceResourceProvider.java:290)
       at railo.commons.io.res.type.datasource.DatasourceResource.attr(DatasourceResource.java:64)
       at railo.commons.io.res.type.datasource.DatasourceResource.exists(DatasourceResource.java:114)
       at railo.runtime.config.ConfigWebUtil._getExistingFile(ConfigWebUtil.java:180)
       at railo.runtime.config.ConfigWebUtil.getExistingResource(ConfigWebUtil.java:168)
       at railo.runtime.MappingImpl.(MappingImpl.java:92)
       at railo.runtime.config.ConfigWebFactory.loadMappings(ConfigWebFactory.java:1196)
       at railo.runtime.config.ConfigWebFactory.load(ConfigWebFactory.java:287)
       at railo.runtime.config.ConfigWebFactory.newInstance(ConfigWebFactory.java:198)
       at railo.runtime.engine.CFMLEngineImpl.loadJSPFactory(CFMLEngineImpl.java:151)
       at railo.runtime.engine.CFMLEngineImpl.getCFMLFactory(CFMLEngineImpl.java:215)
       at railo.runtime.engine.CFMLEngineImpl.serviceCFML(CFMLEngineImpl.java:232)
       at railo.loader.servlet.CFMLServlet.service(CFMLServlet.java:32)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
       at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:491)
       at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:367)
       at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:185)
       at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
       at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:689)
       at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:391)
       at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:146)
       at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
       at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)
       at org.mortbay.jetty.Server.handle(Server.java:285)
       at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:457)
       at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:751)
       at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:500)
       at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:209)
       at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:357)
       at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:329)
       at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:475)
    Powered by Jetty://
  • 6 oyunlar // Jun 27, 2010 at 1:32 AM

    it works fine, i got now errors when I restart Railo