迁移 jboss 到 tomcat - javax.naming.NamingException

Migration jboss to tomcat - javax.naming.NamingException

我们正在将 Struts 1.2 项目从 JDK 1.6 迁移到 1.8,并将应用程序服务器 jboss5.1 迁移到 tomcat 8.0。数据库连接工作正常,初始页面正在加载和填充一些数据。但是当我执行任何操作(提交按钮)时,它会抛出异常 javax.naming.NamingException: Cannot create resource instance 相同的代码在 jboss 5.1 中工作,但在 Tomcat 8 中出现错误。

但我怀疑错误的原因是 UserTransaction 需要在 context.xml 中单独定义,但不确定。请告诉我。

UserTransaction ut = (UserTransaction) myCntxt.lookup("java:comp/UserTransaction");

如果需要定义 UserTransaction,请告诉我如何在 Tomcat 服务器中配置?

在 META-INF/context 下的 context.xml 中添加了数据库配置。xml

<Resource name="jdbc/Test1" 
   auth="Container" 
   type="javax.sql.DataSource" 
   maxTotal="100" 
   maxIdle="30" 
   maxWaitMillis="10000" 
   username="xxxxx" 
   password="xxxxx" 
   driverClassName="oracle.jdbc.OracleDriver" 
   url="jdbc:oracle:thin:@xxxx:1527:xxxx" />

<Resource name="jdbc/Test2" 
   auth="Container" 
   type="javax.sql.DataSource" 
   maxTotal="100" 
   maxIdle="30" 
   maxWaitMillis="10000" 
   username="xxxxx" 
   password="xxxxx" 
   driverClassName="oracle.jdbc.OracleDriver" 
   url="jdbc:oracle:thin:@xxxx:1527:xxxx" />

在 {project}/WEB-INF/web 下的 web.xml 中添加了数据源引用。xml

<resource-ref>
   <description>Oracle Datasource</description>
   <res-ref-name>jdbc/Test1</res-ref-name>
   <res-type>javax.sql.DataSource</res-type>
   <res-auth>Container</res-auth>
</resource-ref>

<resource-ref>
   <description>Oracle Datasource</description>
   <res-ref-name>jdbc/Test2</res-ref-name>
   <res-type>javax.sql.DataSource</res-type>
   <res-auth>Container</res-auth>
</resource-ref>

示例代码

public static void dbInit(String test1dataSourceReference,String test2dataSourceReference) throws Exception
    {

        Context ctx = new InitialContext();
            tds =(DataSource) ctx.lookup("java:comp/env/" + test1dataSourceReference);
            cds = (DataSource) ctx.lookup("java:comp/env/" + test2dataSourceReference);

    }

堆栈跟踪:

Dec 25, 2019 7:28:07 AM org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.taglib.html.LocalStrings', returnNull=true
Dec 25, 2019 7:28:07 AM org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.util.LocalStrings', returnNull=true
Dec 25, 2019 7:28:07 AM org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.taglib.html.LocalStrings', returnNull=true
Dec 25, 2019 7:28:07 AM org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.taglib.html.LocalStrings', returnNull=true
Dec 25, 2019 7:28:08 AM org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.taglib.logic.LocalStrings', returnNull=true
Dec 25, 2019 7:28:08 AM org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.taglib.html.LocalStrings', returnNull=true
Dec 25, 2019 7:28:08 AM org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.taglib.html.LocalStrings', returnNull=true
Dec 25, 2019 7:28:22 AM org.apache.struts.action.RequestProcessor processException
WARNING: Unhandled Exception thrown: class javax.naming.NamingException
Dec 25, 2019 7:28:22 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [action] in context with path [/policy] threw exception [javax.naming.NamingException: Cannot create resource instance] with root cause
javax.naming.NamingException: Cannot create resource instance
    at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:96)
    at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:847)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:158)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:835)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:172)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:157)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at com.db.DbQuery.saveAppRejValue(DbQuery.java:798)
    at com.actions.PolicyAction.executeDisplay(PolicyAction.java:298)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
    at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

由于 TomEE Comparison page 表示 Tomcat 不支持 JTA (JSR 907):

您要么需要删除 JTA 代码,要么考虑像 Wildfly 这样的另一个 EE 服务器作为更简单的升级路径。

您可以在您的 Tomcat 中部署第 3 方 TransactionManager 解决方案(即 Atomikos),我们已经使用旧版本的 Tomcat 完成了它。我们现在 运行 Struts1(和 Struts2)JBoss 6 上的应用程序,因此代码与更新的 JBoss 兼容,后者提供了 JTA 实现。

您可以从代码中移除事务边界(保留 ORM 和 DAO 代码),但我不确定这是一个好主意,因为数据不一致是一个风险。

如果您想尽量减少更改,我会尝试升级到较新的 JBoss 版本。
更好的 medium-term 解决方案是集成 Spring 事务管理(如果您不需要处理跨多个资源的事务)。
long-term 解决方案是重构 Struts1(可能是史诗般的努力,但我必须提到它)。

祝你好运。

贝佩