有个数据库通用类 DBUtil
里面有个public static Connection getConnection()方法,因为JSP 是多线程响应客户请求,那么,getConnection()是不需要同步?如果不同步,会不会出现线程安全问题?
里面有个public static Connection getConnection()方法,因为JSP 是多线程响应客户请求,那么,getConnection()是不需要同步?如果不同步,会不会出现线程安全问题?
[email protected] 谢谢~!
public class DBTool { public String version="DBTool v1.0";
public DBTool()
{
}
public Connection connection() throws Exception
{
DBInf dbinf = new DBInf();
dbinf.load();
String driver = dbinf.getDriver(),
url = dbinf.getUrl(),
user = dbinf.getUser(),
pwd = dbinf.getPwd();
Class.forName(driver).newInstance();
return DriverManager.getConnection(url, user, pwd);
}
public static void main(String args[])
{
/*
try
{
DBTool dbx = new DBTool();
Connection con = dbx.connection();
}
catch(Exception e)
{
e.printStackTrace();
}
*/
}
}class DBInf
{
String driver, url, user, pwd;
public DBInf()
{
}
public void load() throws Exception
{
InputStream ins = getClass().getResourceAsStream("/DBCfg/DBCfg.txt");
Properties p = new Properties();
p.load(ins);
driver = p.getProperty("dbdriver");
url = p.getProperty("dburl");
user = p.getProperty("dbuser");
pwd = p.getProperty("dbpwd");
ins.close();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getDriver() {
return driver;
}
}
JAVA是线程安全的,不同用户不会受到先后干扰的。