Fun with mappings and resources: Part 3 DB Resources

Posted by Mark Drew on code on April 14, 2009

Tagged under railo,getrailo

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.

 




comments powered by Disqus