带有 JNDI 数据源的 Spring Boot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26504913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:08:30  来源:igfitidea点击:

Spring Boot with JNDI Data Source

springjndispring-boot

提问by johnmmcparland

I have a new Spring Boot web application that I want to connect to a JNDI data source (a MySQL database defined in Tomcat's context.xml).

我有一个新的 Spring Boot web 应用程序,我想连接到 JNDI 数据源(在 Tomcat 的 context.xml 中定义的 MySQL 数据库)。

However when I attempt this, I always get the following exception;

但是,当我尝试这样做时,我总是遇到以下异常;

org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database url for database type NONE. If you want an embedded database please put a supported on on the classpath.

This is despite my pom.xml containing the MySQL connector

尽管我的 pom.xml 包含 MySQL 连接器

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.test</groupId>
<artifactId>twojndi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>Two JNDI Data Sources</name>
<description>Two JNDI Data Sources Example</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.8.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
            </exclusion>
            <exclusion>
                <artifactId>tomcat-jdbc</artifactId>
                <groupId>org.apache.tomcat</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
    </dependency>
</dependencies>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>org.test.twojndi.Application</start-class>
    <java.version>1.7</java.version>
</properties>

<build>
    <finalName>${artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

I have defined my application.properties as follows to use the jndi-name property.

我已如下定义我的 application.properties 以使用 jndi-name 属性。

spring.datasource.jndi-name=java:comp/env/jdbc/twojndi_ds1
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

However despite that it seems Spring believes that an in-memory database should be used.

然而尽管如此,Spring 似乎认为应该使用内存数据库。

I am able to connect to the MySQL database if I define my application.properties as so

如果我这样定义我的 application.properties,我就可以连接到 MySQL 数据库

spring.datasource.url=jdbc:mysql://localhost:3306/twojndi_ds1
spring.datasource.username=twojndi
spring.datasource.password=twojndi
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

Can anyone help me connect to JNDI with Spring Boot?

谁能帮我用 Spring Boot 连接到 JNDI?

回答by dunni

As commented by M. Deinum, JDNI lookup is implemented in Spring Boot 1.2, current version is 1.2.0.M2.

正如 M. Deinum 所评论的,JDNI 查找是在 Spring Boot 1.2 中实现的,当前版本是 1.2.0.M2。

If you want to do it with Spring Boot 1.1, you can define a bean like this:

如果你想用 Spring Boot 1.1 来做,你可以像这样定义一个 bean:

@Bean
public DataSource dataSource() {
    JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
    jndiObjectFactoryBean.setJndiName("jdbc/jndidatasource");
    try {
        jndiObjectFactoryBean.afterPropertiesSet();
    } catch (NamingException e) {
        LOGGER.error("Error while retrieving datasource with JNDI name jdbc/jndidatasource", e);
    }
    return (DataSource) jndiObjectFactoryBean.getObject();
}

回答by wilbert

For me it worked the following configuration, guided by the recipe exposed in this link, but as they said before the same work with versions of spring boot 1.2 or more

对我来说,它按照此链接中公开的配方进行了以下配置,但正如他们之前所说,与 spring boot 1.2 或更高版本相同

public class DomainAndPersistenceJndi {
private JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
    hibernateJpaVendorAdapter.setShowSql(true);
    hibernateJpaVendorAdapter.setDatabase(Database.INFORMIX);
    hibernateJpaVendorAdapter.setDatabasePlatform("org.hibernate...");
    return hibernateJpaVendorAdapter;
}

@Bean(name = "dataSourcejndi")
public DataSource dataSourcejndi() throws NamingException {
    JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
    bean.setJndiName("java:jboss/datasources/..." );
    bean.setProxyInterface(DataSource.class);
    bean.setLookupOnStartup(false);
    bean.afterPropertiesSet();
    return (DataSource) bean.getObject();
}

@Bean(name = "entityManagerFactoryJndi")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryJndi(@Qualifier("dataSourcejndi") DataSource dataSource) {

    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource( dataSource);
    em.setPackagesToScan(EntidadBase.class.getPackage().getName());
    em.setJpaVendorAdapter(jpaVendorAdapter());
    em.setPersistenceUnitName("BaseDSjdni");

    em.afterPropertiesSet();

    return em;      
}   

}

}