Announcement Announcement Module
Collapse
No announcement yet.
Multiple application contexts causing JTA issues Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple application contexts causing JTA issues

    Hi,

    I'm working on a web application that has somewhat of an interesting architecture. We have our own middleware/messaging layer which transports data via oagi bods (raw xml).

    So my web app acts as a typical web app does, starts the application context via the ContextLoader servlet, but it also acts as a message consumer. This is where the issues come into play. I have a separate thread that is listening for any messages (bods) to be sent, if one is sent I need to call a method within a spring controlled class, so naturally I would need to provide some way to hook into the application context.

    At first this seemed like an easy thing, BUT since this call is spawned via the listening thread (and not a web request) I don't have any access to pull the current application context out of the servlet context. First is there any easy way to do this? I could mimic a request and gain access to it that way, but that seems hokey and not the way to go. The other alternative is that I read the spring conifguration files and create another instance of the application context (via ClassPathXmlApplicationContext). This is the route I have taken, but have run into issues with the JTA jndi location. This is the error I get when attempting to start up the 2nd application context :

    Code:
    Initialization of bean failed; nested exception is org.springframework.transaction.TransactionSystemException: JTA UserTransaction is not available at JNDI location [java:comp/UserTransaction]; nested exception is javax.naming.NameNotFoundException: Name "comp/UserTransaction" not found in context "java:"
    I think that this is since I have two spring application contexts open, which may be an absolute sin for all you J2EE purists out there

    If anyone has any ideas that would be much appreciated.

    Thanks,
    Dave

  • #2
    What app server are you using? Search the forums too. I think I've seen this sort of question a couple of times before.

    Dino

    Comment


    • #3
      Originally posted by hucmuc
      What app server are you using? Search the forums too. I think I've seen this sort of question a couple of times before.

      Dino
      I'm using Websphere, so I am also using the websphere transaction manager that spring supplies :

      Code:
      	<bean id="websphereTransactionManager" 
      		class="org.springframework.transaction.jta.WebSphereTransactionManagerFactoryBean"/>
      	
      	<bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
      		<property name="transactionManager">
      			<ref local="websphereTransactionManager" />
      		</property>
      	</bean>
      I'll take a look throughout the forums for anything similar. Do you happen to remember any specifics from when you've seen it before?

      Thanks,
      Dave

      Comment


      • #4
        The following link shows an example of how to share the same instance application context instance :

        http://forum.springframework.org/showthread.php?t=14607

        Could be worthwhile to look at.

        Note: you can start the "listening thread" from the application context, so you can inject all the dependencies there. e.g
        Code:
          <bean id="whatever"
                class="com.mycompany.product.ListenerConroller"
                init-method="start" destroy-method="destroy">
            <property name="bean">
              <bean class="anotherBean"/>
            </property>
          </bean>


        Dino
        Last edited by robyn; May 14th, 2006, 08:32 PM.

        Comment


        • #5
          Just thought I'd let you know that I solved the issue with the suggestions made in the above linked thread.

          I created a beanRefContext.xml file which lists the various xml configuration files that make up my application context.

          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
          
          <beans>
              <bean name="mainApplicationContext" class="org.springframework.context.support.ClassPathXmlApplicationContext">
                  <constructor-arg>
                     <list>
                       <value>/WEB-INF/dataAccessContext-local.xml</value>
                       <value>/WEB-INF/applicationContext.xml</value>
                     </list>
                  </constructor-arg>
              </bean>
          </beans>
          Then I mapped that file and its contents to the ContextLoaderServlet in my web.xml :

          Code:
          	<context-param>
          		<param-name>contextConfigLocation</param-name>
          		<param-value>
          			/WEB-INF/dataAccessContext-local.xml /WEB-INF/applicationContext.xml
          		</param-value>
          	</context-param>
              <context-param>
                  <param-name>locatorFactorySelector</param-name>
                  <param-value>classpath*&#58;/WEB-INF/beanRefContext.xml</param-value>
              </context-param>
              <context-param>
                  <param-name>parentContextKey</param-name>
                  <param-value>mainApplicationContext</param-value>
              </context-param>
          Once I did that I was able to get a handle to my application context via my messaging thread with the following code :

          Code:
          	public static BeanFactory getApplicationContext&#40;&#41; &#123;
          		return ContextSingletonBeanFactoryLocator.getInstance&#40;BEAN_REF_CONTEXT_PATH&#41;
          			.useBeanFactory&#40;BEAN_REF_CONTEXT_KEY&#41;.getFactory&#40;&#41;;
          	&#125;
          I appreciate the help guys!! I hope this will help others with the same problem.

          Thanks,
          Dave

          Comment


          • #6
            Hi Dave,

            Seems to make sense, but I am wondering how you are defining the following two constants :

            BEAN_REF_CONTEXT_KEY
            BEAN_REF_CONTEXT_PATH

            Thanks
            -dav0

            Comment

            Working...
            X