7 Replies Latest reply on Aug 28, 2002 4:12 PM by dsundstrom

    Many SQL for getXXX method

    jaime

      Hi

      I have a CMP entity bean and I make a finder method "Collection finByName( )".

      When I execute the finder inside a method of a class (not a session or entity bean), the finder executes without problems, but when I narrow each item of the collection to my remote class and I execute "getXXX", one SQL is executed for each item in the collection, it's ver, very slow!!!!!.

      But, if I excute the finder inside a method of a session bean, an iterate for each item in the collection, narrow each item to my remote interface and execute "getXXX", only one SQL is execute for all the items in the collection, it's right that I neen!!!!

      Somebody knows what is the problem???

      JBoss 3.0
      Win2000 Proffesional
      JDK 1.3

      J@imeS

      Jaime Salvador

        • 1. Re: Many SQL for getXXX method
          dsundstrom

          You have discovered why transactions are required. The system must reload data at the begining or each transaction, because it may have changed since the last transaction comitted. If you want to use a POJO, you will need to manage the transaction by hand with a UserTransaction.

          • 2. Re: Many SQL for getXXX method
            jaime

            Ok,

            but how I can find an example of using UserTransaction???

            Thanks

            JaimeS

            • 3. Re: Many SQL for getXXX method
              jtorres

              The following code snippet shows the use of a UserTransaction:

              [pre]
              //obtain a user transaction
              InitialContext ctx = new InitialContext();
              UserTransaction ut = null;
              try{
              ut = (UserTransaction)ctx.lookup("UserTransaction");

              //start transaction
              ut.begin();

              ... some code ...

              //commit the transaction
              ut.commit();
              }catch(Exception e){
              if(ut != null){
              //rollback if an unexpected exception occurs
              ut.rollback();
              }
              [/pre]

              Hope this helps

              • 4. Re: Many SQL for getXXX method
                tdang

                In which package is the class UserTransaction located?

                Thanks!

                • 5. Re: Many SQL for getXXX method
                  jtorres

                  UserTransaction is in javax.transaction. If you are using JBoss 3.x, you can reference the jar in $JBOSS_HOME/server/default/lib/jboss-j2ee.jar.

                  • 6. Re: Many SQL for getXXX method
                    jaime

                    Thanks
                    It's work perfectly but I have some limitation.

                    If I try to use my EJB inside a JSP file, I need begin and end a transaction, it's not like me. read some articles and I found that another solution is set COMMIT-OPTION to A, but I set this option in the xml file, but I restar JBoss and I have the same problem (with out a transaction).

                    Where I need to set COMMIT-OPTION to A value???

                    Thanks

                    JaimeS

                    • 7. Re: Many SQL for getXXX method
                      dsundstrom

                      You set commit options in the jboss.xml file.

                      I strongly suggest you don't manualy manage transactions in you code. It is not good desing. Instead you could create a servlet filter that starts a tx on the way in and commits it on the way out. Also you need to check the current state of the tx before comitting, and you need a ton of exception handling code. Another option is to use value objects for you beans and access them in the jsp.