Is the DriverManager class a Singleton in Java? -
from this source read that:
you may have experience working jdbc drivers. example, classloader attempts load , link driver class in "org.gjt.mm.mysql" package. if successful, static initializer called.
class.forname("org.gjt.mm.mysql.driver"); connection con = drivermanager.getconnection(url,"mylogin", "mypassword");
let's see why need class.forname() load driver memory. jdbc drivers have static block registers drivermanager , drivermanager has static initializer only.
the mysql jdbc driver has static initializer looks this:
static { try { java.sql.drivermanager.registerdriver(new driver()); } catch (sqlexception e) { throw new runtimeexception("can't register driver!"); } }
does mean drivermanager singleton class?
it's not singleton. it's pure utility class, static methods. there 0 instance of class. singleton have 1 instance of class, , have access instance call instance methods on it.
java documented, , it's open-source. can @ source of class understand how works.
Comments
Post a Comment