spring boot+jndi+oracle

近期一個產品 客戶方需要用webLogic部署,但是部署後發現 事務失效,最後找到原因需要用jndi方式連接數據庫。

1.登錄weblogic管理端配置jndi,這個網上有教程可以參考https://www.cnblogs.com/xdp-gacl/p/4201094.html

 

2.在代碼裏修改數據源,並添加配置

spring:
  datasource:
    jndi-name: test #jndi名稱

#添加weblogic配置
weblogic:
  url: //weblogic管理端地址  t3://www.baidu.com
  jndiName: //配置的jndi名稱
  webLogicName: // weblogic登錄用戶名
  password:  //登錄密碼

3.代碼增加 dataSource配置類

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jndi.JndiObjectFactoryBean;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@Slf4j
public class DataSourceConfig {

    @Value("${weblogic.url}")
    private String webLogicUrl;

    @Value("${weblogic.jndiName}")
    private String weblogicJndiName;

    @Value("${weblogic.webLogicName}")
    private String weblogicLogicName;

    @Value("${weblogic.password}")
    private String weblogicPassword;

    @Bean(name = "dataSource")
    public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
        JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        properties.put(Context.PROVIDER_URL, webLogicUrl);
        //weblogic賬號
        properties.put(Context.SECURITY_PRINCIPAL,weblogicLogicName);
        //weblogic密碼
        properties.put(Context.SECURITY_CREDENTIALS, weblogicPassword);
        bean.setJndiEnvironment(properties);
        bean.setResourceRef(true);
        bean.setJndiName(weblogicJndiName);
        bean.setProxyInterface(DataSource.class);
        bean.setLookupOnStartup(false);
        bean.afterPropertiesSet();
        return (DataSource) bean.getObject();

    }

}

這樣就完美解決了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章