Set jdbc interceptor using DriverManager class 使用DriverManager类设置jdbc拦截器

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

Set jdbc interceptor using DriverManager class

问题

我正在使用 DriverManager.getConnection(url, prop) 来获取连接。我尝试使用以下方式来注入 JDBC 拦截器,但没有生效。

Properties prop = new Properties();
...
prop.setProperty("jdbcInterceptors", "com.amazonaws.xray.sql.mysql.TracingInterceptor;");

然而,当我们尝试通过数据源(DataSource)来实现时,它可以工作。

import org.apache.tomcat.jdbc.pool.DataSource;
DataSource source = new DataSource();
source.setUrl("url");
source.setUsername("user");
source.setPassword("password");
source.setDriverClassName("com.mysql.jdbc.Driver");
source.setJdbcInterceptors("com.amazonaws.xray.sql.mysql.TracingInterceptor;");

不确定使用 DriverManager 属性时出了什么问题。

英文:

I am using DriverManager.getConnection(url, prop) to get the connection. I am trying to inject the jdbc interceptors using properties like below but it is not working.

Properties prop = new Properties();
...
prop.setProperty("jdbcInterceptors", "com.amazonaws.xray.sql.mysql.TracingInterceptor;");

However, when we try to do via datasource it is working.

import org.apache.tomcat.jdbc.pool.DataSource;
DataSource source = new DataSource();
source.setUrl("url");
source.setUsername("user");
source.setPassword("password");
source.setDriverClassName("com.mysql.jdbc.Driver");
source.setJdbcInterceptors("com.amazonaws.xray.sql.mysql.TracingInterceptor;");

Not sure what is wrong with DriverManager properties.

答案1

得分: 3

这些拦截器是Tomcat的org.apache.tomcat.jdbc.pool.DataSourceProxy及其子类org.apache.tomcat.jdbc.pool.DataSource的功能。这不是JDBC本身的功能,也不是您正在使用的JDBC驱动程序的功能,因此唯一的访问方式是通过Tomcat数据源。

简而言之,它不适用于DriverManager,因为这个功能在DriverManager中不存在。

英文:

These interceptors are a feature of the Tomcat org.apache.tomcat.jdbc.pool.DataSourceProxy and its subclass org.apache.tomcat.jdbc.pool.DataSource. This is not a feature of JDBC itself, nor a feature of the JDBC driver you're using, so the only way to access it is through a Tomcat data source.

In short, it doesn't work with DriverManager because this feature doesn't exist in DriverManager.

huangapple
  • 本文由 发表于 2020年8月11日 11:36:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63351123.html
匿名

发表评论

匿名网友

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

确定