提交 7f737de9 编写于 作者: A ascrutae

1.修改Pluin的编译级别 2. 支持H2数据库 3. 修复JDBC插件在JDK1.8的bug

上级 1e3fe831
......@@ -36,8 +36,8 @@
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.7</source>
<target>1.7</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
......
......@@ -35,6 +35,12 @@
<version>10.2.0.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.192</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
......@@ -18,9 +18,7 @@ public class JDBCPluginDefine extends BootPluginDefine {
protected void boot() throws BootException {
try {
Class<?> classes = Class.forName("java.sql.DriverInfo");
Constructor<?> constructor = classes.getDeclaredConstructor(Driver.class);
constructor.setAccessible(true);
Object traceDriverInfo = constructor.newInstance(new TracingDriver());
Object traceDriverInfo = newDriverInfoInstance(classes);
Field field = DriverManager.class.getDeclaredField("registeredDrivers");
field.setAccessible(true);
@SuppressWarnings("unchecked")
......@@ -34,4 +32,16 @@ public class JDBCPluginDefine extends BootPluginDefine {
TracingDriver.registerDriver();
}
}
private Object newDriverInfoInstance(Class<?> classes) throws NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException {
//DriverInfo 只有一个构造函数
Constructor constructor = classes.getDeclaredConstructors()[0];
constructor.setAccessible(true);
// JDK 1.7 DriverInfo 只有一个Driver入参构造函数
if (constructor.getParameterTypes().length == 1){
return constructor.newInstance(new TracingDriver());
}
// JDK1.8 DriverInfo 有两个入参的构造函数(Driver, DriverAction)
return constructor.newInstance(new TracingDriver(), null);
}
}
......@@ -5,4 +5,5 @@ jdbc:microsoft:sqlserver=com.microsoft.jdbc.sqlserver.SQLServerDriver
jdbc:sqlserver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc:jtds:sqlserver=net.sourceforge.jtds.jdbc.Driver
jdbc:db2=com.ibm.db2.jcc.DB2Driver
jdbc:informix-sqli=com.informix.jdbc.IfxDriver
\ No newline at end of file
jdbc:informix-sqli=com.informix.jdbc.IfxDriver
jdbc:h2=org.h2.Driver
\ No newline at end of file
package test.ai.cloud.skywalking.plugin.h2;
import com.ai.cloud.skywalking.plugin.TracingBootstrap;
import com.ai.skywalking.testframework.api.RequestSpanAssert;
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class H2JDBCTest {
@Test
public void testMySqlJDBC() throws InvocationTargetException, NoSuchMethodException, ClassNotFoundException, IllegalAccessException {
TracingBootstrap.main(new String[]{H2JDBCTest.class.getName()});
}
public static void main(String[] args) throws ClassNotFoundException, SQLException, InterruptedException {
Class.forName("org.h2.Driver");
String url = "jdbc:h2:" + H2JDBCTest.class.getResource("/") + "test.db";
Connection con = DriverManager.getConnection(url);
con.setAutoCommit(false);
PreparedStatement p0 = con.prepareStatement("select 1 from dual where 1=?");
p0.setInt(1, 1);
p0.execute();
con.commit();
con.close();
RequestSpanAssert.assertEquals(
new String[][]{{"0", "jdbc:h2:" +H2JDBCTest.class.getResource("/") + "test.db" + "(null)", "preaparedStatement.executeUpdate:select 1 from dual where 1=?"},
{"0", "jdbc:h2:" +H2JDBCTest.class.getResource("/") + "test.db" + "(null)", "connection.commit"},
{"0", "jdbc:h2:" +H2JDBCTest.class.getResource("/") + "test.db" + "(null)", "connection.close"},}, true);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册