2011-10-12 156 views
0

我正在开发一个J2EE应用程序,它将接收来自移动应用程序的请求并执行一些数据库操作以向用户显示数据。Tomcat连接池方法

因此数据库连接池是对我,我重视我的J2EE application.It我META-INF文件夹声明context.xml如下所示

<Context debug="0" 
    reloadable="true" crossContext="false" privileged="true" cookies="true" > 

    <Resource name="jdbc/servicedb" 
       auth="Container" 
       type="javax.sql.DataSource" 
       driverClassName="oracle.jdbc.OracleDriver" 
       username="XXXXXX" 
       password="XXXXXX" 
       url="jdbc:oracle:thin:@XXXXXXXXXXX:XXXX:XXXXXXX" 
       initialSize="10" 
       maxActive="100" 
       maxIdle="50" 
       minIdle="10" 
       suspectTimeout="60" 
       timeBetweenEvictionRunsMillis="30000" 
       minEvictableIdleTimeMillis="60000"/> 
</Context> 

这是从我的连接的方法类返回一个连接

public static Connection getConnection() 
    { 
     Connection dbConnection = null; 

     //loading from the dao.properties file 
     DAOProperties properties = new DAOProperties("com.jndi"); 
     String url = properties.getProperty(PROPERTY_URL, true); 
     String driverClassName = properties.getProperty(PROPERTY_DRIVER, false); 
     String password = properties.getProperty(PROPERTY_PASSWORD, false); 
     String username = properties.getProperty(PROPERTY_USERNAME, password != null); 

    // If driver is specified, then load it to let it register itself with DriverManager. 
     if(driverClassName !=null){ 
      try 
      { 
       Class.forName(driverClassName); 
       dbConnection = DriverManager.getConnection(url, username, password); 

      }catch(ClassNotFoundException e){ 
       // Could not find the database driver 
       e.printStackTrace(); 
       System.out.println("database driver error"); 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
     } 
    // Else assume URL as DataSource URL and lookup it in the JNDI. 
     else{ 
      Context initContext; 
      DataSource ds; 
      try { 
       initContext = new InitialContext(); 
       Context envContext = (Context)initContext.lookup(JNDI_ROOT); 
       if(envContext!=null){ 
        ds = (DataSource)envContext.lookup(url); 
        if(ds!=null){ 
         try { 
          dbConnection = ds.getConnection(); 
         } catch (SQLException e) { 
          e.printStackTrace(); 
         } 
        } 
       } 
      } catch (NamingException e) { 
       e.printStackTrace(); 
      } 
     } 
     return dbConnection; 
    } 

DAOProperties是装载属性file.I一个包装类把它从here

我将结束这样的

public static void close(Connection connection) { 
     if (connection != null) { 
      try { 
       connection.close(); 
      } catch (SQLException e) { 
       System.err.println("Closing Connection failed: " + e.getMessage()); 
       e.printStackTrace(); 
      } 
     } 
    } 

连接,最后我现在用的连接polulate这样

public loadlist(){ 
     Connection dbConnection = null; 
     try{ 
      dbConnection = Connection.getConnection(); 
     if(dbConnection !=null){ 

      System.out.println("Connected to database"); 
      LOGGER.debug("Successfully connected to database"); 
     else{ 
       System.out.println("No Connection Exists"); 
       LOGGER.debug("unable to connect to database"); 
     } 
     }catch (SQLException e) { 
      // Could not connect to the database 
      e.printStackTrace(); 
      System.out.println("database connection failed"); 
      LOGGER.debug("database connection failed due to SQLException"); 
     }finally{ 
      Connection.close(dbConnection); 
      LOGGER.debug("connection closed"); 
     } 
     return result; 
    } 

刚才我已经通过this在连接池了一个清单。

我有疑问我是否可以关闭我的连接或将它返回给池?

我也想知道如何返回连接池?

请帮忙。

回答

2

假设你的代码将在getConnection()方法其他路径(即,它JNDI查找,而不是使用的DriverManager的),你的连接池的使用是正确的。

您不能选择关闭连接并将其返回到池中。如果您从池中获得连接,则close()将返回到池(而不是关闭它)。没有其他选择。您只能关闭自己创建的连接。

+0

换句话说:处理来自池的'Connection'就像处理非池化连接一样:只要在完成使用时调用'close()'。 –

+0

@Codo-谢谢你的回复。你说的是tomcat只会管理连接。只要看看'close'方法[here](http://balusc.blogspot.com/2008/07/dao -tutorial-data-layer.html#HowAboutConnectionPooling) – Sreeram

+0

@Sreeram:在你的评论中链接的'close()'方法是一个说明连接池是如何实现的,而不是你应该如何使用它或者你应该如何实现你的DAO类。 Tomcat管理何时创建连接,何时重新使用连接,何时将连接返回到池以及何时关闭连接。 – Codo

0

我不明白你为什么要编写自己的连接池。

我建议使用这种方法并使用由Tomcat管理的JNDI数据源和由其他人编写的池。 Apache有一个很好的DBCP - 为什么写自己的?

+0

@ duffymo-感谢您的回复。我现在只遵循JNDI查找方法。我怀疑是关于''close'方法返回连接池。 – Sreeram