7 Replies Latest reply on Jan 28, 2009 10:22 PM by vanyatka

    Multiple (non-xa)datasources?

    dustismo

      Hello,


      I have read a couple of posts about people using multiple datasources within their seam app without using xa-datasource.  How is this possible?  I would love to stop using xa datasources, but I don't see how. 


      For instance:
      link


      the post was never resolved, but no-one suggested that the approach is simply not possible..  Whenever I try to configure this way, I get:


      Could not enlist in transaction on entering meta-aware object
      



      see: link


      any help?


      -Dustin

        • 1. Re: Multiple (non-xa)datasources?
          gjeudy

          I would recommend using XA datasources if you plan to have multiple datasources participating in the same transaction.


          If you absolutely need to keep local datasources then you need to access each one from a separate transaction.


          If you can't do this then you can enable multiple local datasources in the same JBossTS transaction, even though this is not recommended (don't be fooled you have no atomicity guarantees with this approach).


          You would have to enable

          <property name="com.arjuna.ats.jta.allowMultipleLastResources" value="true"/>

          in your jbossjta-properties.xml file.


          Read further info about this here.

          • 2. Re: Multiple (non-xa)datasources?
            dustismo

            Excellent, thanks for the response.


            I have a secondary datasource that is used very rarely, so I don't mind handling the transactions for it by hand.  I have no idea how to do that though.  the following does not work:



            EntityManager em = (EntityManager)Component.getInstance("secondEntityManager",true);
            em.getTransaction().begin();
            Object result = em.createNativeQuery( "SELECT blah blah").getSingleResult();
            em.getTransaction().commit();
            




            I get


            java.lang.IllegalStateException: JTA EntityManager cannot access a transactions
                 at org.hibernate.ejb.AbstractEntityManagerImpl.getTransaction(AbstractEntityManagerImpl.java:316)
                 at org.jboss.seam.persistence.EntityManagerProxy.getTransaction(EntityManagerProxy.java:110)
                 at com.trendrr.servlet.api.admin.TorrentSearchStats.process(TorrentSearchStats.java:25)
            
            ...
            



            Obviously I can't use the transaction from Transaction.instance(), so how can I create a new useable transaction?


            any more help?


            -Dustin


            • 3. Re: Multiple (non-xa)datasources?
              dustismo
              I got it too work..

              To answer my own question:

              add transaction-type equal to RESOURCE_LOCAL and change to non-jta-data-source in persistence.xml


              `
              <persistence-unit name="second-unit" transaction-type="RESOURCE_LOCAL">
                    <provider>org.hibernate.ejb.HibernatePersistence</provider>
                    <non-jta-data-source>java:/secondDatasource</non-jta-data-source>

              </persistence-unit>
              `
              • 4. Re: Multiple (non-xa)datasources?
                accless

                Thx for this hint!

                • 5. Re: Multiple (non-xa)datasources?
                  squirrel

                  Well I must still be missing something.  I've tried this and other techniques, but it still seems to try to bind all the entities to every datasource.  The entities are all annotated with


                  @PersistenceContext(unitName="MainDatasource")
                  



                  But I still get


                  [ServiceController] Problem starting service persistence.units:ear=myproject-ear.ear,unitName=OtherDatasource
                  javax.persistence.PersistenceException: org.hibernate.HibernateException: Missing table: MainDatasourceTable
                  



                  Any other hints?  What else am I missing?


                  • 6. Re: Multiple (non-xa)datasources?
                    gjeudy

                    I think thats a different problem. Please post a detailed usecase in a new post.

                    • 7. Re: Multiple (non-xa)datasources?
                      vanyatka

                      I would recommend using XA datasources if you plan to have multiple datasources participating in the same transaction.

                      Is it still true if one of the datasources is read-only?



                      If you absolutely need to keep local datasources then you need to access each one from a separate transaction.

                      Correct me if I'm wrong, but you cannot suspend or create a new transaction if you're using Seam POJOs:


                      /**
                       * Transaction propagation strategies for Seam JavaBean 
                       * components. Note that unlike EJB3 components, there 
                       * are no strategies for suspending transactions.
                       * 
                       * @author Gavin King
                       *
                       */
                      public enum TransactionPropagationType
                      {
                         REQUIRED,
                         MANDATORY, 
                         SUPPORTS,
                         NEVER;