JTA数据源java配置在春天

时间:2014-11-22 09:50:31

标签: java spring spring-mvc jboss datasource

所以在过去的几天里我一直在尝试使用spring配置JTA数据源,但无论hpw多少我谷歌我都无法找到如何做到这一点。

这是我当前的数据源配置,但这适用于本地数据源配置

    @Bean
    public DataSource dataSource() {
        final BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/expenses");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }

这是我的jboss 8的standalone.xml中的数据源配置:

 <datasource jndi-name="java:/ExpensesDataSource" pool-name="ExpensesDataSourcePool" enabled="true" use-java-context="true">
                    <connection-url>jdbc:mysql://localhost:3306/expenses</connection-url>
                    <driver>mysql</driver>
                    <security>
                        <user-name>root</user-name>
                        <password>root</password>
                    </security>
                </datasource>

显然现在我有双重代码,因为我晚上没有使用我的jboss中的数据源。我想配置我的jta数据源以使用jndi lookup来使用这个数据源。

编辑#2:

所以最终我找到了这段代码:

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

然而,这给了我以下的堆栈跟踪:

Caused by: org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'java:/ExpensesDataSource'; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

编辑#2:

我看了一下,发现我需要设置一些初始环境参数,得到这个:

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");

我尝试localhostlocalhost:1099http://localhost:1099作为Context.PROVIDER_URL,其中没有一个包括这个。给我以下错误:

Caused by: javax.naming.CommunicationException: Failed to connect to server localhost:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.ConnectException: Connection refused: connect]]
    at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:274)
    at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1533)
    ... 96 more
Caused by: javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.ConnectException: Connection refused: connect]
    at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:248)
    ... 97 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:328)
    at org.jnp.interfaces.TimedSocketFactory.createSocket(TimedSocketFactory.java:84)
    at org.jnp.interfaces.TimedSocketFactory.createSocket(TimedSocketFactory.java:77)
    at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:244)
    ... 97 more

1 个答案:

答案 0 :(得分:0)

我在JBOSS 8中看到你有以下命名空间

  • 的java:比较
  • 的java:模块
  • 的java:应用
  • 的java:全球
  • 的java:JBoss的
  • 的java:/

所以在你的春季申请中

<jee:jndi-lookup id="datasource" jndi-name="java:global/ExpensesDataSource"></jee:jndi-lookup>

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

在standalone.xml配置中,请尝试按照此snippet

进行操作