Results 1 to 6 of 6

Thread: How to get another(Context) web app's SessionFactory?

  1. #1
    Join Date
    Sep 2004
    Posts
    5

    Unhappy How to get another(Context) web app's SessionFactory?

    Hi everyone:

    Could I get a sessionfactory of another webapp's? (I use Tomcat5) I have two web applications,WebApp1 and WebApp2. They use hibernate and spring but using different databases.
    Could Spring load another webapp's context to get the sessionfactory?

    It is possible to bind sessionfactory1 to webapp1's context jndi tree and get it in webapp2, spring could do this?
    for example: Webapp1 use hibernate1.cfg.xml as the following:


    Code:
    <session-factory> 
    ...................................................................... 
          <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property> 
          <property name="connection.url">jdbc:mysql://192.168.1.17:3306/user</property> 
    ............................................................................

    webapp2 use hibernate2.cfg.xml in another application:
    Code:
    <session-factory> 
    ...................................................................... 
          <property name="connection.driver_class">com.oracle.jdbc.OracleDriver</property> 
          <property name="connection.url">jdbc:oracle@thin:192.168.1.17:3306/user</property> 
    ............................................................................

    Now , I want to operate the sessionfactory1 in webapp2
    How to do it? I mean that it is possible to get the sessionfactory1 in webapp2? I want to bind the sessionfactory1 to jndi tree,then how to get the sessionfactory1 in my second webapp2?
    Thks!

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    You can't access the 'space' of another webapp directly that why you need to publish the information outside of it. JNDI is such a solution and besides using the support from spring (like JndiObjectFactoryBean) you can use HB facilities which can bind the session factory after building at your desired location.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Sep 2004
    Posts
    5

    Question :(

    Quote Originally Posted by costin
    You can't access the 'space' of another webapp directly that why you need to publish the information outside of it. JNDI is such a solution and besides using the support from spring (like JndiObjectFactoryBean) you can use HB facilities which can bind the session factory after building at your desired location.
    Thank you! I know how to build the sessionFactory to local webapp's jndi tree,and I could get the sessionfactory from local web context(jndi tree). But how to get another webapp's jndi handler? I mean that how to get webapp1's jndi object from webapp2's context? Could I do this by this way:?
    How does the JndiObjectFactoryBean object help me?

    Code:
    Context webapp2Context= new InitialContext(); 
    sessionFactory = 
    (SessionFactory) webapp2Context.lookup("webapp1/jndi/HibernateSessionFactory");
    Could I get the jndi tree cross Tomcat context?
    But it seems don't work. Have you do it using JndiObjectFactoryBean? Thks!

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Tomcat JNDI implementation is read-only - see their documentation for more details. Spring samples provide a lot of good examples, including JNDI; here is a snippet:

    <!-- Main JNDI DataSource for J2EE environments -->
    <!-- Refers to the main database, containing product and account data -->
    <!-- (see dataAccessContext-local.xml for an alternative) -->
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName" value="java:comp/env/jdbc/jpetstore"/>
    </bean>
    Basically, you need in webapp1 the jndi reference of webapp2 - you'll use that to get a hold of the HB SessionFactory and reuse it inside your application.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  5. #5
    Join Date
    Sep 2004
    Posts
    5

    Question No

    Quote Originally Posted by costin
    Tomcat JNDI implementation is read-only - see their documentation for more details. Spring samples provide a lot of good examples, including JNDI; here is a snippet:



    Basically, you need in webapp1 the jndi reference of webapp2 - you'll use that to get a hold of the HB SessionFactory and reuse it inside your application.
    No. You couldn't find another webapp's jndi reference in the current context. If I do it,TOmcat will throws Exception :
    Code:
    root cause 
    
    javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
    	org.apache.naming.NamingContext.lookup(NamingContext.java:814)
    	org.apache.naming.NamingContext.lookup(NamingContext.java:184)
    	org.apache.naming.NamingContext.lookup(NamingContext.java:825)
    	org.apache.naming.NamingContext.lookup(NamingContext.java:184)
    	org.apache.naming.NamingContext.lookup(NamingContext.java:825)
    	org.apache.naming.NamingContext.lookup(NamingContext.java:197)
    	org.apache.naming.SelectorContext.lookup(SelectorContext.java:183)
    	javax.naming.InitialContext.lookup(InitialContext.java:347)
    	org.apache.jsp.testjndi_jsp._jspService(testjndi_jsp.java:47)
    	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
    For example, I bind a datasource to webapp1's context, if in another webapp2,I use the following code the reference the datasource ,tomcat will report exception:
    Code:
    // In webapp2 's  context  do :
    Context ctx=new InitialContext();
    DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/webapp1");
    String sql="select * from user_info";
    Connection con=ds.getConnection();
    Statement stm=con.createStatement();
    ResultSet rs=stm.executeQuery(sql);
    Because the jndi object have not been bind to the tomcat global context.So you couldn't find it in another webapp.

  6. #6
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Iyo, Tomcat doesn't have a full blown JNDI implementation. I assume you can plug another one (there are some free implementations available) or use another web container like jetty.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Posting Permissions

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