Results 1 to 6 of 6

Thread: Multiple application contexts causing JTA issues

  1. #1
    Join Date
    Sep 2005
    Location
    St. Louis, MO USA
    Posts
    25

    Default 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. #2
    Join Date
    Aug 2004
    Posts
    107

    Default

    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

  3. #3
    Join Date
    Sep 2005
    Location
    St. Louis, MO USA
    Posts
    25

    Default

    Quote 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

  4. #4
    Join Date
    Aug 2004
    Posts
    107

    Default

    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 at 07:32 PM.

  5. #5
    Join Date
    Sep 2005
    Location
    St. Louis, MO USA
    Posts
    25

    Default

    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

  6. #6
    Join Date
    Oct 2006
    Posts
    13

    Default

    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

Similar Threads

  1. Replies: 6
    Last Post: Oct 4th, 2010, 01:02 AM
  2. Replies: 1
    Last Post: Dec 19th, 2007, 03:21 PM
  3. Multiple init sequences in a web application
    By YanivShaya in forum Container
    Replies: 1
    Last Post: Jul 7th, 2005, 04:23 PM
  4. Questioning the core component
    By Martin Kersten in forum Swing
    Replies: 6
    Last Post: Feb 21st, 2005, 03:45 AM
  5. Replies: 3
    Last Post: Dec 14th, 2004, 07:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •