5 Replies Latest reply on Jul 23, 2015 9:31 PM by igarashitm

    Transaction over SQL reference bindings

    ozkin

      A composite reference can have only one reference binding. Imagine your component has to execute two SQL statements as one transaction (for example, two DELETEs) and you are using Camel SQL binding. Then you will have to use two composite references, each having one SQL binding (with one SQL statement per binding).

      How could you implement transactional behaviour within your component while you are invoking those two references?

        • 1. Re: Transaction over SQL reference bindings
          ozkin

          In Camel there is "transacted()" block which one could you. But what if your component (which is invoking those multiple SQL bindings) is just a Bean?

          • 2. Re: Transaction over SQL reference bindings
            ozkin

            Heh..if I use TransactionManager directly in the Bean component implementation then it seems it solves the problem. That's the code:

            // For SY1.1.0-Final

            import javax.inject.Inject;

            import javax.naming.InitialContext;

            import javax.transaction.TransactionManager;

            import org.switchyard.component.bean.Reference;

            import org.switchyard.component.bean.Service;

            @Service(MyService.class)

            public class MyServiceBean implements MyService {

                private static final String JNDI_TRANSACTION_MANAGER = "java:jboss/TransactionManager";

                @Inject @Reference("SqlBinding1")

                private FooService sqlBinding1;

                @Inject @Reference("SqlBinding2")

                private FooService sqlBinding2;

                @Override

                public void foo() throws Exception {

                    TransactionManager tm = (TransactionManager) new InitialContext().lookup(JNDI_TRANSACTION_MANAGER);

                    try {

                        tm.begin();

                        sqlBinding1.execute();

                        sqlBinding2.execute();

                        tm.commit();

                    } catch (Exception e) {           

                        tm.rollback();

                        throw e;

                    }

                }

            }

            Could someone confirm that this is ok'ish approach?

            • 3. Re: Transaction over SQL reference bindings
              jorgemoralespou_2

              Hi Max,

              Switchyard provides with declarative transactions, so there is no need to manage them yourself.

               

              In the component you have to define the transaction type (local or global) and on the interface you need to select tree behavior you want.

               

               

              Here's some doc fsw-documentation/transactions.adoc at master · jorgemoralespou/fsw-documentation · GitHub

              • 4. Re: Transaction over SQL reference bindings
                ozkin

                Thanks for reply Jorge. Of course, I've read about SY's declarative transactions, but I didn't find it fitting my case (or I misunderstood something).

                My example above is just oversimplification. The gateway for my service is not transactional. My bean implementation also doesn't participate in any transactions. I just need one operation (foo() above) to implement a transactional behaviour for a couple of SQL binding invocations. I considered this to be noManagedTransaction case (or simply None) and let application itself manage transactions.. Any suggestions how it can be improved maybe?

                • 5. Re: Transaction over SQL reference bindings
                  igarashitm

                  You can use managedTransaction.Global on your bean. If no transaction is propageted through service binding, then SY runtime will begin new global transaction before execution and commit it after execution.

                  https://docs.jboss.org/author/display/SWITCHYARD/Policy

                   

                  Please take a look at policy-transaction demo for example.

                  switchyard/quickstarts/demos/policy-transaction at master · jboss-switchyard/switchyard · GitHub