获取SpringBoot 2中的OracleDataSource

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

Obtain an OracleDataSource in SpringBoot 2

问题

这是我尝试过的。

  1. @Repository
  2. public class MyClass{
  3. @Autowired
  4. NamedParameterJdbcTemplate jdbcTemplate;
  5. public void myMethod(){
  6. try{
  7. //OracleDataSource ods = new OracleDataSource(); // This works but is obviously not Spring
  8. OracleDataSource ods = (OracleDataSource) jdbcTemplate.getJdbcTemplate().getDataSource(); // This fails
  9. ods.setURL(url);
  10. ods.setUser(user);
  11. ods.setPassword(pass);
  12. // ...
  13. }
  14. catch(Exception e){
  15. System.out.println("In Exception");
  16. e.printStackTrace();
  17. }
  18. }
  19. }

Application.properties:

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

Error message:

  1. In Exception
  2. 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.

  1. @Repository
  2. public class MyClass{
  3. @Autowired
  4. NamedParameterJdbcTemplate jdbcTemplate;
  5. public void myMethod(){
  6. try{
  7. //OracleDataSource ods = new OracleDataSource(); // This works but is obviously not Spring
  8. OracleDataSource ods = (OracleDataSource) jdbcTemplate.getJdbcTemplate().getDataSource(); // This fails
  9. ods.setURL(url);
  10. ods.setUser(user);
  11. ods.setPassword(pass);
  12. ...
  13. catch(Exception e){
  14. System.out.println("In Exception");
  15. e.printStackTrace();
  16. }
  17. }

}

Application.properties:

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

Error message:

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

答案1

得分: 0

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

  1. 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:

  1. 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:

确定