我写了一个从DATASOURCE取得CONNECTION的工具类 那么DATASOURCE可以多线程并发访问吗????还有,为什么我不能直接写成STATIC类?????????? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 …又是一个没有注释的class... 我得设计有人说有问题,是这样,有人说应该用单态模式,那个模式我知道,就是使系统中只有一个实例,但是这里就有一个疑问,不知道用不用担心: 如果这样实现的话(安我上面的实现,把DATASOURCE写成类成员),DATASOURCE就是被多线程并发访问的,这样做有没有问题?如果不能这样,就只有去掉这个类的所有成员变量,全部在方法里面生成,这样做问题我想是没有了,不过好像效率有问题。请大家发表一下对于这个问题的看法,或者给我一个你们的工具类的实现好不好? 池的访问.客户程序可以调用getInstance()方法访问本类的唯一实例.static synchronized public pool getInstance() { if (instance == null) { instance = new pool(); } return instance; } DATASOURCE并不是实际池的实现, 只是提供给你的访问接口直接把DATASOURCE声明成static 就行了, 整个项目就一个DATASOURCE实例就行 参考servicelocator模式不就行了吗。 ServiceLocator模式:单态模式: static { serviceLocatorRef = new ServiceLocator(); } private ServiceLocator(){} public static ServiceLocator getInstance(){ return serviceLocatorRef;}对连接进行缓存,然后需要的时候提取。public Connection getDBConn(int pServiceId) throws ServiceLocatorException{ String serviceName = getServiceName(pServiceId); Connection conn = null; try { if (dataSourceCache.containsKey(serviceName)) { DataSource ds = (DataSource) dataSourceCache.get(serviceName); conn = ( (DataSource) ds).getConnection(); return conn; } else { Context ctx = new InitialContext(); DataSource newDataSource = (DataSource) ctx.lookup(serviceName); dataSourceCache.put(serviceName, newDataSource); return newDataSource.getConnection(); } }catch (SQLException e) { throw new ServiceLocatorException( "A SQLErro has occured in ServiceLocator.getDBConn() method. "); } catch (NamingException e) { throw new ServiceLocatorException( "A JNDI Naming has occured in ServiceLocator.getEJBHome() method. "); } catch (Exception e) { throw new ServiceLocatorException( "General exception in ServiceLocator.getEJBHome() method. "); } } 请问你的jdbc/sqlserverDB参数怎么配置的呀 spring mvc 配置出错,启动不了,求大牛指点 求助 找不到类 如何查看tomcat容器的会话连接数 relace括号问题 jdbc 访问sql server2005报我的jdbc驱动类找不到 hibernate search整合spring 菜鸟 interface 困惑 在struts开发中,JSP页面不需要接收Form值的时候也需要写ActionForm吗? 大家谈一下学习J2EE的经验(参与有分) 软件项目中 关于表与表之间实体与实体之间的关联设计问题。 WIN98如何象2000一样可以右击我的电脑属性那样设置path和classpath?? 菜鸟请求帮忙!!!!!!!!
是这样,有人说应该用单态模式,那个模式我知道,就是使系统中只有一个实例,但是这里就有一个疑问,不知道用不用担心: 如果这样实现的话(安我上面的实现,把DATASOURCE写成类成员),DATASOURCE就是被多线程并发访问的,这样做有没有问题?
如果不能这样,就只有去掉这个类的所有成员变量,全部在方法里面生成,这样做问题我想是没有了,不过好像效率有问题。
请大家发表一下对于这个问题的看法,或者给我一个你们的工具类的实现好不好?
static synchronized public pool getInstance() {
if (instance == null) {
instance = new pool();
}
return instance;
}
DATASOURCE并不是实际池的实现, 只是提供给你的访问接口直接把DATASOURCE声明成static 就行了, 整个项目就一个DATASOURCE实例就行
单态模式:
static {
serviceLocatorRef = new ServiceLocator();
}
private ServiceLocator(){}
public static ServiceLocator getInstance(){
return serviceLocatorRef;
}
对连接进行缓存,然后需要的时候提取。
public Connection getDBConn(int pServiceId) throws ServiceLocatorException{ String serviceName = getServiceName(pServiceId);
Connection conn = null;
try {
if (dataSourceCache.containsKey(serviceName)) {
DataSource ds = (DataSource) dataSourceCache.get(serviceName);
conn = ( (DataSource) ds).getConnection();
return conn;
}
else {
Context ctx = new InitialContext();
DataSource newDataSource = (DataSource) ctx.lookup(serviceName);
dataSourceCache.put(serviceName, newDataSource);
return newDataSource.getConnection();
}
}catch (SQLException e) {
throw new ServiceLocatorException(
"A SQLErro has occured in ServiceLocator.getDBConn() method. ");
}
catch (NamingException e) {
throw new ServiceLocatorException(
"A JNDI Naming has occured in ServiceLocator.getEJBHome() method. ");
}
catch (Exception e) {
throw new ServiceLocatorException(
"General exception in ServiceLocator.getEJBHome() method. ");
}
}