那么DATASOURCE可以多线程并发访问吗????
还有,为什么我不能直接写成STATIC类??????????

解决方案 »

  1.   

    …又是一个没有注释的class...
      

  2.   

    我得设计有人说有问题,
    是这样,有人说应该用单态模式,那个模式我知道,就是使系统中只有一个实例,但是这里就有一个疑问,不知道用不用担心: 如果这样实现的话(安我上面的实现,把DATASOURCE写成类成员),DATASOURCE就是被多线程并发访问的,这样做有没有问题?
    如果不能这样,就只有去掉这个类的所有成员变量,全部在方法里面生成,这样做问题我想是没有了,不过好像效率有问题。
    请大家发表一下对于这个问题的看法,或者给我一个你们的工具类的实现好不好?
      

  3.   

    池的访问.客户程序可以调用getInstance()方法访问本类的唯一实例.
    static synchronized public pool getInstance() {
        if (instance == null) {
          instance = new pool();
        }
        return instance;
      }
      

  4.   


    DATASOURCE并不是实际池的实现, 只是提供给你的访问接口直接把DATASOURCE声明成static 就行了, 整个项目就一个DATASOURCE实例就行
      

  5.   

    参考servicelocator模式不就行了吗。
      

  6.   

    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. ");
        }
      }
      

  7.   

    请问你的jdbc/sqlserverDB参数怎么配置的呀