11 Replies Latest reply on May 9, 2008 7:56 AM by haagenhasle

    JBossESB and Hibernate

    haagenhasle

      Hi!

      I want to use Hibernate in my services, both to store data into a database, and as a listener to trigger new services when data is inserted into a table.

      I found an quickstart-example of how I can use hibernate as a listener. When I deploy it into the ESB, I see that most of the actual hibernate-files end up in the lib-directory, not in deploy. Why is this? Does is have to be like this, or can I do it differently, so that everything ends up in deploy?

      I found no examples of how to use hibernate to store data (messages) into a database, but I guess that's just regular Hibernate-coding. If there are any best practises I should be aware of, please let me know! :-)

      Regards, Haagen

        • 1. Re: JBossESB and Hibernate
          tcunning

          Hi Haagen,

          I think one of the JARs was deployed into lib rather than deploy to deal with problems that came up with hot-deploy - see http://jira.jboss.com/jira/browse/JBESB-721 for details. I think the scenario you are proposing has two separate applications dealing with the same hibernate POJOs, so you might want to think think about doing it this way.

          As for best practices, I'd read up on Hibernate Interceptors and how they are used and in what situations they are fired off.

          • 2. Re: JBossESB and Hibernate
            haagenhasle


            Thanks for the quick reply, and for the top about Hibernate Interceptors. It's been a while since I last used Hibernate, I'll be sure to read up on it.

            We don't really hot-deploy anything, so I'm not concerned about the issue you refer to.

            But if I want to use the same pojos from two different applications I would need to put the hibernate-stuff in a jar-file in /lib, is that what you are saying?

            It might be a stupid question, but what makes two applications "separate"? In some of your quickstars you have a .war-file inside an .esb-file - is that two separate applications?

            I've put a jar-file that two esb-archives both used into the /deploy-directory before, and that seemed to work out just fine. (But I thing it kind of broke the hot-deploy functionality if I remember correctly..) Can I do the same with Hibernate if I don't care about hot-deploy?

            Regards, Haagen

            • 3. Re: JBossESB and Hibernate
              tcunning

              Haagen, I just tried putting the jar in the deploy directory with the quickstart and it seemed fine, although the lib directory seems like a more appropriate place to put a library shared between applications. The deploy directory is watched by the hot deployment service for new or updated content. The lib directory is the location for static java libraries that will not be hot deployed (http://docs.jboss.com/jbossas/guides/installguide/r1/en/html_single/#dirs). I think you're safer putting your shared jar in the lib directory in terms of best practices (if someone overwrites your jar in the deploy directory it might have unintended effects) - but you might also have a strong reason for putting it in the deploy directory. So I guess it's up to you and what you feel comfortable with.

              • 4. Re: JBossESB and Hibernate
                haagenhasle

                It seems to me that there is a bit of magic that is going on in the Hibernate-example in the quickstarts (at least I'm not able to figure it all out..) :)

                What is the connection between the hibernate-listener and the jms-listener? Does the hibernate-listener/gateway resend the message into as jms before it get gets picked up be the first action? If so, at which queue?

                The jms-provider uses a queue A, but I can't find where that queue is defined. And the jms-bus' selector is another mystery, the "fromHelloworldHibernateAction" seems to me to be just a random word.. :-)

                In the documentation I read about composers and stuff, but I don't see any use of them in the example..

                And a last question: Is Hibernate only able to listen on events (onSave etc) that it is the source of itself? Or can I insert data into a database through a standard sql-client and have Hibernate/the ESB pick it up?

                Regards, Haagen

                • 5. Re: JBossESB and Hibernate
                  tcunning

                  "And a last question: Is Hibernate only able to listen on events (onSave etc) that it is the source of itself? Or can I insert data into a database through a standard sql-client and have Hibernate/the ESB pick it up?"

                  It's only going to reply to Hibernate events. If you wanted to do something that also saw standard sql-client events, you'd need to write some sort of database trigger integration. I've never tried it but I'm fairly sure if this is what you needed that you could do it in Oracle, where you can embed java code.

                  From the sounds of it, queue A is extraneous code that is leftover from something that was removed, I'll take a better look at it later.

                  • 6. Re: JBossESB and Hibernate
                    haagenhasle

                    I don't really think queue A is a leftover.. I tried to remove the entire jms-provider and the jms-listener, and then the ESB complained that the service didn't have a listener that wasn't a gateway. And the hibernate-listener needs gateway="true". But I'm not sure if the jms-stuff is really used, or if it's just "sitting there" to fulfill som configuration requirement.. (But that wouldn't make any sense, so I guess it's used.)

                    So I still wonder about the connection between the hibernate-listener and the jms-listener (or providers..) and the name of the selector.

                    Thanks for clarifying about the Hibernate-listener only listening to Hibernate-events. But instead of writing a database trigger as you suggest, can't I use the SQL-listener instead? The quickstart that uses it inserts data into the database from ant, and a comment somewhere indicates that the SQL-listener polls the database for changes. (Which is what I first thought hibernate did too..)

                    Regards, Haagen

                    • 7. Re: JBossESB and Hibernate
                      haagenhasle

                      After searching the forum I found I'm not the only one who has had problems with this.. :)
                      http://www.jboss.com/index.html?module=bb&op=viewtopic&t=111719&postdays=0&postorder=asc&start=10

                      If the answer on that thread is still valid, it seems the message is passed from the sql-gateway to jms somehow, before it reaches the action pipeline. That is fine by me (although I don't really understand why it is necessary), but the selector and queue-definitions are still a puzzle to me.

                      Regards, Haagen

                      • 8. Re: JBossESB and Hibernate
                        haagenhasle

                        I've played around a bit with the code, and I see that the selector can be whatever I want, is works anyway.

                        Regards, Haagen

                        • 9. Re: JBossESB and Hibernate
                          haagenhasle

                          I have defined my sql-bus like this:

                          <sql-bus busid="haagenSQLChannel" >
                           <sql-message-filter
                           tablename="message"
                           order-by="uuid"
                           message-id-column="uuid"
                           status-column="status"
                           post-delete="false"
                           error-delete="false"
                           />
                          </sql-bus>

                          When I insert anything into the message-table, I see that the ESB picks it up, and the status changes from P to D. All good.

                          But then I tried to insert an exception into my processing-action:
                          if (true) {
                           throw new ActionProcessingException("I'm going down...");
                          }


                          I would have thought that the status would now have changed from P to E, but no, it still ended up being set to D.

                          Is this a bug, or am I doing something wrong?

                          Regards, Haagen

                          • 10. Re: JBossESB and Hibernate
                            tfennelly

                            Hi Haagen.

                            AFAIK, the transaction boundaries will not span the DB and JMS endpoints. Once the SQL Gateway picks up the message and delivers it to the JMS endpoint (the message aware action pipeline), that's the scope of that and the SQL Listener has done it's job properly (no errors). When the error happens in the action pipeline, that's a whole different ball game. You can't use the P/D/E flags as indicators of the success or failure of the global process... you need to handle it in some other way.

                            • 11. Re: JBossESB and Hibernate
                              haagenhasle

                              Even when I'm using the jms-jca-provider? I guess we would be talking JTA and distributed transactions, but that's okay with me.. :-)

                              But okay, I guess I'll set the post-delete flag back to true, and use the exception-method in the action to push the data back into the database if an error occurs.

                              Hmm... Wait a second.. I see that in the 4.3.CR1-release you have a quickstart called helloworld_tx_sql_action, where you use two sql-providers (one is a gateway and one is not), and where the one that's not a gateway has a property transacted set to true. Is this a new feature of 4.3.CR1? This could be what I need.


                              Regards, Haagen