获取SpringBoot 2中的OracleDataSource

huangapple go评论154阅读模式
英文:

Obtain an OracleDataSource in SpringBoot 2

问题

这是我尝试过的。

@Repository
public class MyClass{

    @Autowired
    NamedParameterJdbcTemplate jdbcTemplate;

    public void myMethod(){
        try{
            //OracleDataSource ods = new OracleDataSource();                                           // This works but is obviously not Spring
            OracleDataSource ods = (OracleDataSource) jdbcTemplate.getJdbcTemplate().getDataSource();   // This fails

            ods.setURL(url);
            ods.setUser(user);
            ods.setPassword(pass);

            // ...
        }
        catch(Exception e){
            System.out.println("In Exception");
            e.printStackTrace();
        }
    }
}

Application.properties:

spring.datasource.url=jdbc:oracle:thin:@//${ORA_HOST}:${ORA_PORT}/${ORA_SID}
spring.datasource.username=${USER}
spring.datasource.password=${PASS}

Error message:

In Exception
java.lang.ClassCastException: com.zaxxer.hikari.HikariDataSource cannot be cast to oracle.jdbc.pool.OracleDataSource
英文:

Is it possible to retrieve a OracleDataSource from the default SpringBoot 2 Hikari connection pool using a NamedParameterJdbcTemplate object?
Using Java 8, Oracle 11g (ojdbc6-11.2.0.1.jar) and gradle
This is what i've tried.

@Repository
public class MyClass{

@Autowired
NamedParameterJdbcTemplate  jdbcTemplate;

public void myMethod(){
try{
	//OracleDataSource ods = new OracleDataSource();											// This works but is obviously not Spring
	OracleDataSource ods = (OracleDataSource) jdbcTemplate.getJdbcTemplate().getDataSource();   // This fails
			
	ods.setURL(url);
	ods.setUser(user);
	ods.setPassword(pass);

	...
	catch(Exception e){
			System.out.println("In Exception");
			e.printStackTrace();
	}
}

}

Application.properties:

spring.datasource.url=jdbc:oracle:thin:@//${ORA_HOST}:${ORA_PORT}/${ORA_SID}
spring.datasource.username=${USER}
spring.datasource.password=${PASS}	

Error message:

In Exception
java.lang.ClassCastException: com.zaxxer.hikari.HikariDataSource cannot be cast to oracle.jdbc.pool.OracleDataSource

答案1

得分: 0

我不认为这是可能的(或者是必要的)。最简单的方法是对连接对象进行unwrap()操作,该对象已经连接到了数据库:

Connection conn = this.jdbcTemplate.getJdbcTemplate().getDataSource().getConnection().unwrap(OracleConnection.class);
英文:

I don't think this is possible (or neccessary). The easiest way is to unwrap() a connection object, which has already connected to the dB:

Connection conn =  this.jdbcTemplate.getJdbcTemplate().getDataSource().getConnection().unwrap(OracleConnection.class);

答案2

得分: 0

只是一个查询,就像这样,你是如何获得OracleConnection.class的?因为在我的情况下,OracleConnection.class下面有波浪线,并且没有可导入的包。

英文:

Just a query like, how are you able to get OracleConnection.class, because in my case I get squiggly line underneath OracleConnection.class and there are no packages available to import.

huangapple
  • 本文由 发表于 2020年4月9日 18:12:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61118813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定