5 Replies Latest reply on Jun 16, 2008 5:36 AM by Cloves Almeida

    Asynchronous Database Queries

    Stephan B Newbie


      just a quick question: How can one make asychronous database queries?

      I got 2 databases, say users and contracts. At one point in the application it is necessary to query results from borth DBs at once (get me the user data and contracts of the customer).
      So, to optimize performance (because the DB is probably the bottleneck of the application), one could do the queries parallel. The session bean would have to wait for both results and then return/outject them.

      How could this be done in/with Seam (not AJAX!)?


        • 1. Re: Asynchronous Database Queries
          Cloves Almeida Newbie

          Check chapter 19 of docs
          which deals with asynchronicity.

          From there, you can easily call asynchronous methods while having full access to the seam context.

          Just some not-so-well documented caveats:

          1) The implementation is based on either the EJB Timer service or Quartz. I've found that the Quartz implementation is far more reliable.

          2) If you use anything but Quartz configured for in-memory job storage, You'll probably run into transaction problems if your datasources are not XA. Check the wiki for help configuring them. Also, MySQL Connectors and XA Transactions is lottery game! If thing blow for no reason, you might want to check tem.

          • 2. Re: Asynchronous Database Queries
            Stephan B Newbie

            Well, thank you for your reply.

            I am familiar with that part of the documentation. Maybe me question was not clear enough.

            In the documentation you got the example of calling a void method. In my case, I want to call 2 methods which both return a value (e.g. user and contract data).

            I am not sure you to build a system that waits for the calls and only then proceeds...

            • 3. Re: Asynchronous Database Queries
              Dan Allen Master

              What you are probably looking for is a method that returns a handle to the task, which must be done using the EJBTimer implementation (or Quartz if it supports it). Then you can keep checking that task until it is finished and read off the data. I would check the EJB documentation for this one, because this is actually separate from Seam. Seam just facilitates the use of the asynchronous mechanism.

              By the way, you left out an implementation. By default, Seam uses the Java 5 concurrent library to execute asynchronous methods. There are three limitations with it: it is not persistence (meaning the task might never execute), it does not support scheduling, and (I think) you cannot monitor the task.

              • 4. Re: Asynchronous Database Queries
                Stephan B Newbie

                Thanks very much for the reply!

                • 5. Re: Asynchronous Database Queries
                  Cloves Almeida Newbie

                  True. My bad :)

                  BTW, I've posted a patch in JIRA to inject the QuartzTriggerHandle into the context - the same behavior of the EJB Timer implementation.

                  Otherwise, I couldn't make a repeating job cancel itself.