NamingException:jta.UserTransaction [英] NamingException: jta.UserTransaction

查看:153
本文介绍了NamingException:jta.UserTransaction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个直接使用Java Transaction API资源的hibernate应用程序。我试图使用UserTransaction完成一些基本的数据库事务

lockquote
UserTransaction接口定义了允许
应用程序的方法显式管理事务边界

在hibernate.cfg.xml文件中,我有以下属性:

 < session-factory> 
< property name =show_sql> true< / property>
< property name =connection.datasource> java:comp / env / myDataSource< / property>
< property name =jta.UserTransaction> java:comp / UserTransaction< / property>
< property name =transaction.factory_class> org.hibernate.transaction.JTATransactionFactory< / property>

在app的web.xml中,UserTransaction被设置在< ; resource-env-ref>

 < resource-env-ref> 
< description> MyBean实例的对象工厂。< / description>
< resource-env-ref-name> UserTransaction< / resource-env-ref-name>
< resource-env-ref-type> javax.transaction.UserTransaction< / resource-env-ref-type>
< / resource-env-ref>

在代码中,我尝试使用 InitialContext查找UserTransaction 由其 JNDI 名称

  public UserTransaction getTransactionContext()
throws例外
{
if(this.ut == null){
ut =(UserTransaction)new InitialContext()。lookup(java:comp / UserTransaction);
}
return ut;
}

然而,以下异常会被返回:

  03:27:37,530 ERROR [someServlet:555] someMethod中的错误
javax.naming.NamingException:无法在org处创建资源实例
。 apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:117)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
at org.apache.naming.NamingContext。查找(NamingContext.java:843)
在org.apache.naming.NamingContext.lookup(NamingContext.java:154)
在org.apache.naming.NamingContext.lookup(NamingContext.java:831)
在org.apache.naming.NamingContext.lookup(NamingContext.java:154)
在org.apache.naming.NamingContext.lookup(NamingContext.java:831)
在org.apache .naming.NamingContext.lookup(NamingContext.java:168)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:158)

Th应用程序是从一个Weblogic服务器移植过来的,它不能正常工作到Apache Tomcat;话虽如此 - 我需要在 context.xml 中定义 UserTransaction ,以便 getTransactionContext()找到它?如果没有,我该如何在我的代码中正确地返回UserTransaction? 解决方案

从完整的Java EE应用程序中移植应用程序服务器到Tomcat(Web应用程序服务器)需要一些额外的步骤。所有Java EE AS都带有一个JTA事务管理器,它们的事务引擎管理事务绑定和事务解析,如 UserTransaction JNDI上下文。



我推荐你通过关于文章。您可以使用 Bitronix Atomikos ,您需要在Tomcat context.xml中设置UserTransaction和TransactionSyncronizationRegistry。


I have a hibernate application which directly uses Java Transaction API resources. I'm trying to use UserTransaction to accomplish some basic database transactions

The UserTransaction interface defines the methods that allow an application to explicitly manage transaction boundaries

In hibernate.cfg.xml file I have the following properties:

<session-factory>
    <property name="show_sql">true</property>
    <property name="connection.datasource">java:comp/env/myDataSource</property>
    <property name="jta.UserTransaction">java:comp/UserTransaction</property>
    <property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>

In the web.xml of the app, UserTransaction is set in the <resource-env-ref>

<resource-env-ref>
    <description>Object factory for MyBean instances.</description>
    <resource-env-ref-name>UserTransaction</resource-env-ref-name>
    <resource-env-ref-type>javax.transaction.UserTransaction</resource-env-ref-type>
</resource-env-ref>

And in the code, I try to lookup the UserTransaction with InitialContext by its JNDI name

public UserTransaction getTransactionContext()
    throws Exception
    {
        if (this.ut == null) {
            ut = (UserTransaction)new InitialContext().lookup("java:comp/UserTransaction");
        }
    return ut;
    }   

However the following exception gets returned:

03:27:37,530 ERROR [someServlet:555] Error in someMethod
javax.naming.NamingException: Cannot create resource instance
    at org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:117)
    at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:831)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:831)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:158)

This application was ported over from a Weblogic server where it was functioning noramlly to Apache Tomcat; That being said - do I need to define UserTransaction in my context.xml in order for getTransactionContext() to locate it? If not, how can I appropriately return the UserTransaction within my code?

解决方案

Porting an application from a full-blown Java EE application server to Tomcat (web application server) requires some extra steps.

All Java EE AS come with a JTA transaction manager and their transaction engine manages transaction binding and transaction resolving, like the UserTransaction JNDI context.

I recommend you go through the following article. You can use Bitronix or Atomikos, and you need to set both the UserTransaction and the TransactionSyncronizationRegistry in Tomcat context.xml.

这篇关于NamingException:jta.UserTransaction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆