为JBoss 8(WildFly)配置JTA数据源 [英] Configuring a JTA datasource for JBoss 8 (WildFly)

查看:285
本文介绍了为JBoss 8(WildFly)配置JTA数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我读过几次书,如果您使用Java EE容器,则无需将环境参数添加到 InitialContext 即可使用JNDI 。

So, I read several times that if you use a Java EE container, you do not need to add environment params to an InitialContext in order to be able to use JNDI.

所以我尝试了此操作:

    @Bean
    public DataSource dataSource() {
        JndiDataSourceLookup jndiDataSourceLookup = new JndiDataSourceLookup();
        return jndiDataSourceLookup.getDataSource("java:global/ExpensesDataSource");
    }

但是,使用JNDI这样检索数据源给了我 NoInitialContextException ,告诉我指定环境参数。

However, retrieving a datasource using JNDI like this gives me a NoInitialContextException, telling me to specify the environment params.

现在,好吧,所以似乎我认为它可以如此完美地工作是不对的,所以我尝试像这样检索数据源:

Now, okay, so seems I was wrong to think it would work so flawlessly, so I tried retrieving the datasource like this:

    @Bean
    public DataSource dataSource() {
        Properties jndiProperties = new Properties();
        jndiProperties.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
        jndiProperties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
        jndiProperties.put("java.naming.factory.url.pkgs", "org.jboss.naming.org.jnp.interfaces");
        JndiDataSourceLookup jndiDataSourceLookup = new JndiDataSourceLookup();
        jndiDataSourceLookup.setJndiEnvironment(jndiProperties);
        return jndiDataSourceLookup.getDataSource("java:global/ExpensesDataSource");
    }

但是,这给了我 javax.naming.CommunicationException :无法连接到服务器localhost:1099

我也尝试使用 localhost:1099 localhost ,它们都不起作用。

I've also tried just using localhost:1099 or localhost, none of them worked.

所以我的问题是:我是否还需要指定这些属性,因为据我所知JBoss 8是Java EE容器。如果是这样,我需要在此处指定哪个提供者URL?

So my question is: do I even need to specify these properties, since JBoss 8 is a Java EE container to my knowledge. And if so, what provider url do I need to specify here?

推荐答案

如果使用javax.naming.InitialContext,则不要就像您说的那样,无需指定环境参数。例如:

If you use javax.naming.InitialContext, you don't need to specify environment params, just like you said. For example:

InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:jboss/datasources/ExampleDS");

不确定JndiDataSourceLookup的工作方式。

Not sure how JndiDataSourceLookup works..

要查找数据源,可以使用@Resource(在ejb上下文中)注入它

For looking up a datasource, you can inject it using @Resource (inside an ejb context)

@Resource(name= "java:jboss/datasources/ExampleDS")
private Datasource ds;

希望有帮助!

这篇关于为JBoss 8(WildFly)配置JTA数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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