3 Replies Latest reply on Feb 6, 2008 9:59 AM by Pete Muir

    How to specify more than one persistence context in Componen

    geeta sawant Newbie

      Hi
      I am working on Seam 2.0 pojo, toplink, tomcat6. I got it working with one persistence unit where my components.xml and persistence.xml and context.xml file looked like below

      components.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <components xmlns="http://jboss.com/products/seam/components"
       xmlns:core="http://jboss.com/products/seam/core"
       xmlns:persistence="http://jboss.com/products/seam/persistence"
       xmlns:transaction="http://jboss.com/products/seam/transaction"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://jboss.com/products/seam/core
       http://jboss.com/products/seam/core-2.0.xsd
       http://jboss.com/products/seam/transaction
       http://jboss.com/products/seam/transaction-2.0.xsd
       http://jboss.com/products/seam/persistence
       http://jboss.com/products/seam/persistence-2.0.xsd">
      
       <core:init debug="true" />
      
       <core:manager conversation-timeout="120000" />
      
       <transaction:entity-transaction entity-manager="#{em}" />
      
       <persistence:entity-manager-factory name="helloworld" />
      
       <persistence:managed-persistence-context name="em" auto-create="true"
       entity-manager-factory="#{helloworld}" />
      
      </components>
      


      persistence.xml file
      <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd" version="1.0">
       <persistence-unit name="helloworld" transaction-type="RESOURCE_LOCAL">
       <provider>oracle.toplink.essentials.PersistenceProvider</provider>
       <non-jta-data-source>java:comp/env/jdbc/PortalDS</non-jta-data-source>
       <class>Person</class>
       <properties>
       <property name="toplink.logging.level" value="FINE"/>
       <!-- <property name="toplink.ddl-generation" value="drop-and-create-tables"/> -->
       <property name="toplink.session.customizer" value="ToplinkSessionCustomizer"/>
       </properties>
       </persistence-unit>
      </persistence>
      
      


      context.xml
      <Context path="/tomcatoratoplink" docBase="tomcatoratoplink"
       debug="5" reloadable="true" crossContext="true">
      
       <Resource name="jdbc/PortalDS" auth="Container" type="javax.sql.DataSource"
       maxActive="100" maxIdle="30" maxWait="10000"
       username="ghxmonitor_040200" password="ghxmonitor" driverClassName="oracle.jdbc.OracleDriver"
       url="jdbc:oracle:thin:@devdb10.ghx.com:1526:devdb1"/>
      



      Now I made changes to persistence.xml and context .xml file to add one more persistence unit as follows

      persistence.xml
      <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd" version="1.0">
       <persistence-unit name="helloworld" transaction-type="RESOURCE_LOCAL">
       <provider>oracle.toplink.essentials.PersistenceProvider</provider>
       <non-jta-data-source>java:comp/env/jdbc/PortalDS</non-jta-data-source>
       <class>Person</class>
       <properties>
       <property name="toplink.logging.level" value="FINE"/>
       <!-- <property name="toplink.ddl-generation" value="drop-and-create-tables"/> -->
       <property name="toplink.session.customizer" value="ToplinkSessionCustomizer"/>
       </properties>
       </persistence-unit>
      
       <persistence-unit name="Registration" transaction-type="RESOURCE_LOCAL">
       <provider>oracle.toplink.essentials.PersistenceProvider</provider>
       <non-jta-data-source>java:comp/env/jdbc/SFRegistration</non-jta-data-source>
       <properties>
       <property name="toplink.logging.level" value="FINE"/>
       <property name="toplink.session.customizer" value="ToplinkSessionCustomizer"/>
       </properties>
       </persistence-unit>
      </persistence>
      


      context.xml
      <Context path="/tomcatoratoplink" docBase="tomcatoratoplink"
       debug="5" reloadable="true" crossContext="true">
      
       <Resource name="jdbc/PortalDS" auth="Container" type="javax.sql.DataSource"
       maxActive="100" maxIdle="30" maxWait="10000"
       username="ghxmonitor_040200" password="ghxmonitor" driverClassName="oracle.jdbc.OracleDriver"
       url="jdbc:oracle:thin:@devdb10.ghx.com:1526:devdb1"/>
      
      
       <Resource name="jdbc/SFRegistration" auth="Container" type="javax.sql.DataSource"
       maxActive="100" maxIdle="30" maxWait="10000"
       username="reg_api" password="reg_api_pwd" driverClassName="oracle.jdbc.OracleDriver"
       url="jdbc:oracle:thin:@devdb10.ghx.com:1527:devdb1"/>
      
       </Context>
      



      but I don't know how to specify another entity manager in components.xml for persistence unit "Registration".

      Any help appreciated.
      Thanks
      Geeta

        • 1. Re: How to specify more than one persistence context in Comp
          Pete Muir Master

          Add:

          <persistence:entity-manager-factory name="RegistrationFactory" persistence-unit-name="RegistrationFactory" />
          <persistence:managed-persistence-context name="em1" auto-create="true" entity-manager-factory="#{RegistrationFactory}" />


          • 2. Re: How to specify more than one persistence context in Comp
            geeta sawant Newbie

            After adding the above lines I added both entity managers using entity-transaction elements in components.xml file

            <transaction:entity-transaction entity-manager="#{em2}"/>
            
            <transaction:entity-transaction entity-manager="#{em1}"/>
            


            first I tried without precedence attribute but it complained
            java.lang.IllegalStateException: Two components with the same name and precedence: org.jboss.seam.transaction.transaction

            after adding precedence like so

            <transaction:entity-transaction entity-manager="#{em2}" precedence="1"/>
            
             <transaction:entity-transaction entity-manager="#{em1}" precedence="2"/>
            


            it was able to read components.xml file

            Now in pojo I am accessing only one entity-manager as follows

            @In (create=true)
            private EntityManager em2;

            but I am getting following stack trace on accessing the seam page

            java.lang.IllegalStateException: Could not start transaction
             at org.jboss.seam.jsf.SeamPhaseListener.begin(SeamPhaseListener.java:571)
             at org.jboss.seam.jsf.SeamPhaseListener.handleTransactionsBeforePhase(SeamPhaseListener.java:307)
             at org.jboss.seam.jsf.SeamPhaseListener.beforeServletPhase(SeamPhaseListener.java:142)
             at org.jboss.seam.jsf.SeamPhaseListener.beforePhase(SeamPhaseListener.java:116)
             at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:222)
             at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
             at javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
             at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
             at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
             at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
             at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:68)
             at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
             at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
             at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
             at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
             at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
             at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:44)
             at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
             at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
             at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
             at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
             at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
             at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
             at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
             at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
             at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
             at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
             at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
             at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
             at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
             at java.lang.Thread.run(Thread.java:595)
            Caused by: Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0 (Build b41-beta2 (03/30/2007))): oracle.toplink.essentials.exceptions.DatabaseException
            Internal Exception: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Io exception: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=169870080)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4)))))
            Error Code: 0
             at oracle.toplink.essentials.exceptions.DatabaseException.sqlException(DatabaseException.java:290)
             at oracle.toplink.essentials.jndi.JNDIConnector.connect(JNDIConnector.java:135)
             at oracle.toplink.essentials.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:170)
             at oracle.toplink.essentials.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:537)
             at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:206)
             at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:212)
             at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:78)
             at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:111)
             at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:105)
             at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:76)
             at org.jboss.seam.persistence.ManagedPersistenceContext.initEntityManager(ManagedPersistenceContext.java:79)
             at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManager(ManagedPersistenceContext.java:108)
             at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
             at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
             at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
             at java.lang.reflect.Method.invoke(Method.java:585)
             at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
             at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:125)
             at org.jboss.seam.Component.callComponentMethod(Component.java:2074)
             at org.jboss.seam.Component.unwrap(Component.java:2100)
             at org.jboss.seam.Component.getInstance(Component.java:1879)
             at org.jboss.seam.Component.getInstance(Component.java:1832)
             at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:55)
             at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:50)
             at org.jboss.seam.el.SeamELResolver.resolveBase(SeamELResolver.java:166)
             at org.jboss.seam.el.SeamELResolver.getValue(SeamELResolver.java:53)
             at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
             at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:64)
             at org.jboss.el.parser.AstIdentifier.getValue(AstIdentifier.java:44)
             at org.jboss.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
             at org.jboss.seam.core.Expressions$1.getValue(Expressions.java:112)
             at org.jboss.seam.transaction.EntityTransaction.initEntityManager(EntityTransaction.java:66)
             at org.jboss.seam.transaction.EntityTransaction.begin(EntityTransaction.java:78)
             at org.jboss.seam.jsf.SeamPhaseListener.begin(SeamPhaseListener.java:566)
             ... 30 more
            Caused by: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Io exception: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=169870080)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4)))))
             at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1225)
             at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
             at oracle.toplink.essentials.jndi.JNDIConnector.connect(JNDIConnector.java:130)
             ... 62 more
            Caused by: java.sql.SQLException: Io exception: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=169870080)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
             at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
             at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
             at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:334)
             at oracle.jdbc.ttc7.TTC7Protocol.handleIOException(TTC7Protocol.java:3678)
             at oracle.jdbc.ttc7.TTC7Protocol.logon(TTC7Protocol.java:352)
             at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:365)
             at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:547)
             at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:347)
             at org.apache.tomcat.dbcp.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
             at org.apache.tomcat.dbcp.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)
             at org.apache.tomcat.dbcp.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)
             at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)
             ... 64 more
            


            I am sure this is not a TopLink error because it worked when I had only one entry for entity manager in components.xml file.

            Please help.

            Geeta

            • 3. Re: How to specify more than one persistence context in Comp
              Pete Muir Master

              Yes, you can only register one entity-transaction sync at a time. Probably your best bet is to use a full JEE environment, with JTA transaction which will allow you do this ootb.