Articles tagged with TorqueBox

  1. Rails
    15 May 2012
    1. TorqueBox allows running distributed transactions over multiple databases and messaging queues. But what to do, if you also want to operate on the filesystem?
      This can be done with XADisk.

      This post outlines the necessary steps to setup XADisk.
      It will enable you to work on your filesystem and database in distributed transactions from within your Rails application.

    2. Notice

      At the time of writing, this does not work out of the box.
      There is an issue in IronJacamar which prevents deployment of XADisk as a resource adapter.

      Also the XADisk 1.2 Adapter does not comply with the JCA Spec.
      Both issues are resolved and should be fixed in the next releases.

      To work around this, I had to modify the adapter. You can get it here.

    3. Prerequisities

      Verify that TorqueBox and your application work by executing the following tasks from your rails app.

      rake torquebox:check
      rake torquebox:deploy
      rake torquebox:run

      Your application should now be available at localhost:8080.

      To enable full distributed transaction support in PostgreSQL, you’ll need to set max_prepared_transactions to something greater than zero in postgresql.conf, which is the usual default in most installations.

    4. Deploying the XADisk Resource Adapter

      To deploy it, put XADisk.rar in torquebox/jboss/standalone/deployments.

      Next the resource adapter needs to be configured.
      Therefore open torquebox/jboss/configuration/standalone.xml.
      Modify the resource adapters subsystem:

      <subsystem xmlns="urn:jboss:domain:resource-adapters:1.0"> 
                  <config-property name="xaDiskHome"> 
                  <config-property name="instanceId"> 
                          <config-property name="instanceId"> 

      Make sure to modify the property “xaDiskHome” according to your system.
      This is the working directory of the XADisk instance where it stores its transaction logs among other stuff. The instanceId is the name for this instance.
      Also the JNDI name of the connection factory must be configured.

      Start TorqueBox. XADisk should now be successfully deployed.

      The logs should state something similar to this:

      01:30:55,552 INFO  [] (MSC service thread 1-5) JBAS015876: Starting deployment of "XADisk.rar"
      01:30:56,090 INFO  [] (MSC service thread 1-4) JBAS010406: Registered connection factory java:global/xadisk1
      01:30:56,252 INFO  [] (MSC service thread 1-1) JBAS010401: Bound JCA ConnectionFactory [java:global/xadisk1]
      01:30:58,772 INFO  [] (DeploymentScanner-threads - 2) JBAS018559: Deployed "XADisk.rar"
    5. Using the adapter in a Rails app

      To use the resource adapter the connection factory has to be looked up via JNDI.
      The JavaDoc of XADisk lists the supported operations.

      # wrap database and filesystem in one transaction
      TorqueBox.transaction do
        # lookup the xadisk connection factory
        factory = TorqueBox::Naming.context.lookup("java:global/xadisk1")
        # acquire a connection
        conn = factory.connection
          a = Article.create! :title => "test" 
          file = "/opt/filestore/#{}"
          conn.create_file file

      More about distributed transactions in TorqueBox can be found here.