英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论