Alibaba珍藏版mybatis手写文档,Java SPI机制分析

com.mysql.fabric.jdbc.FabricMySQLDriver

提供的每个驱动类占据一行,解析的时候会按行读取,具体使用哪个会根据url来决定;2.简单实例

String url = “jdbc:mysql://localhost:3306/db3”;

String username = “root”;

String password = “root”;

String sql = “update travelrecord set name=‘bbb’ where id=1”;

Connection con = DriverManager.getConnection(url, username, password);

类路径下存在多个驱动包,具体在使用DriverManager.getConnection应该使用哪个驱动类会解析url来识别,不同的数据库有不同的url前缀;3.驱动类加载分析  具体META-INF/services/下的驱动类是什么时候加载的,DriverManager有一个静态代码块:

static {

loadInitialDrivers();println("JDBC DriverManager initialized");

}

private static void loadInitialDrivers() {

String drivers;try {drivers = AccessController.doPrivileged(new PrivilegedAction() {public String run() {return System.getProperty("jdbc.drivers");}});} catch (Exception ex) {drivers = null;}// If the driver is packaged as a Service Provider, load it.// Get all the drivers through the classloader// exposed as a java.sql.Driver.class service.// ServiceLoader.load() replaces the sun.misc.Providers()AccessController.doPrivileged(new PrivilegedAction() {public Void run() {ServiceLoader loadedDrivers = ServiceLoader.load(Driver.class);Iterator driversIterator = loadedDrivers.iterator();/* Load these drivers, so that they can be instantiated.* It may be the case that the driver class may not be there* i.e. there may be a packaged driver with the service class* as implementation of java.sql.Driver but the actual class* may be missing. In that case a java.util.ServiceCon


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部