jython tomcat intitialcontext无法查找上下文jdbc jndi连接

vwkv1x7d  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(239)

我花了很长时间让jython在tomcat(9)下运行,以便对jdbc连接资源进行上下文查找。
我可以获取initialcontext,但是当我尝试任何.lookup()时,我没有得到异常,但是之后的代码似乎都不会执行。
我在server.xml中有一个用于jdbc连接的全局命名资源,在context.xml中有一个context-resourcelink,这些都适用于tomcat下的其他纯java web应用程序。
基于此,我还尝试使用zxjdbc上下文查找函数获得相同的结果[https://www.jython.org/jython-old-sites/archive/21/docs/zxjdbc.html][1]

ctx = InitialContext()
c = ctx.lookup("java:/comp/env")

# result = org.apache.naming.NamingContext@2ccbe380

t = ctx.list("java:comp/env")   

# result = org.apache.naming.NamingContextEnumeration@19797ea4

if t.hasMore(): t.next().getName()
@ result = singe entry of "jdbc"

但是,以下查找尝试不会返回任何内容,似乎会导致所有剩余代码不执行,并且不会引发异常。奇怪的

ds = c.lookup("jdbc/UGOPS")

db = zxJDBC.lookup("jdbc/UGOPS", INITIAL_CONTEXT_FACTORY="com.sun.jndi.fscontext.RefFSContextFactory")

我完全不明白为什么在执行这些之后没有代码,也没有抛出异常。评论这些,一切都会执行。
有没有人能做到这一点并举个例子,或者你能看出我做得不对吗??
测试:getcontext.py

import traceback
import sys 
from javax.naming import Context
from javax.naming import InitialContext
from javax.naming import NamingException
from javax.servlet import ServletException
from javax.servlet.http import HttpServlet
from javax.servlet.http import HttpServletRequest
from javax.servlet.http import HttpServletResponse
from javax.sql import DataSource
from com.ziclix.python.sql import zxJDBC

class getContext( HttpServlet ) :

# ------------------------------------------------------------------------------

  def doGet( self, request, response ) :
    self.doPost( request, response )

  def doPost( self, request, response ) :
    out = response.getWriter()
    response.setContentType( "text/html" )

    out.println( '<DOCTYPE HTML PUBLIC "-//WC3//DTD HTML 4.01 Transitional//EN">' )
    out.println( '<html>' )
    out.println( '<head>' )
    out.println( '<meta name="generator" content="script">' )
    out.println( '<title>' )
    out.println( 'Test Tomcat InitialContext JNDI lookup' )
    out.println( '</title>' )
    out.println( '<base target="_self"/>' )
    out.println( '</head>' )
    out.println( '<body class="content">' )
    out.println( '<div id="results">' )

    try :   
      out.println('get context...')
      ctx = InitialContext()
      out.println('context env...')
      c = ctx.lookup("java:/comp/env")
      out.println(c)
      # org.apache.naming.NamingContext@2ccbe380 

      out.println('context lookup...')

      t = ctx.list("java:comp/env")   
      out.println(t)  
      # org.apache.naming.NamingContextEnumeration@19797ea4   

      # this produces a single .next()  of 'jdbc'
      if t.hasMore():
        out.println('next')
        out.println('<p></p>')
        out.println('<p>' + t.next().getName() + '</p>')        
        # 'jdbc'        
      else:
        out.println('none')

      # this causes no exception but nothing else executes after this ?
      ds = c.lookup("jdbc/UGOPS")

      out.println('<p>get context JNDI...</p>')
      jndiName = "jdbc/UGOPS"
      factory = "com.sun.jndi.fscontext.RefFSContextFactory"

      # try to use the zxJDBC context lookup
      # this causes no exception but nothing else executes after this ?
      db = zxJDBC.lookup(jndiName, INITIAL_CONTEXT_FACTORY=factory)

      out.println('<p>got db...</p>')
      out.print(db)      

      ctx.close()

    except Exception, e :
      out.println('Exception: ')
      out.println('<p>')
      out.println("e.message: " + e.message)  
      exc_type, exc_value, exc_traceback = sys.exc_info()
      out.println(repr(traceback.format_exception(exc_type, exc_value, exc_traceback)) )
      out.println('/p>')
      out.println( "</body></html>" )

    out.println( '</div>' )
    out.println( '</body>' )
    out.println( '</html>' )

server.xml代码段

<GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />

    <Resource name="jdbc/UGOPS" auth="Container" type="javax.sql.DataSource"
              username="*******" password="*******"
              driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
              url="jdbc:sqlserver://localhost:1433;databaseName=UGOPS"
              validationQuery="select 1"
              poolPreparedStatements="true"/>                 
  </GlobalNamingResources>

context.xml代码段

<Context>                                  -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <ResourceLink global="jdbc/UGOPS" name="jdbc/UGOPS" type="javax.sql.DataSource"/>
</Context>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题