META-INF/context.xml配置数据源连接池,获取的Session需要close()吗?

非法字符啊 2011-03-09 11:09:03
问题如下:
1.context.xml配置数据源连接池,配置如下
<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true" crossContext="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource
name="jdbc/dhlDB"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://xxx.xx.xxx.xxx:xxxx;databaseName=DHL"
maxActive="200"
maxldle="60"
maxWait="10000"
username="mps"
password="mps"
/>
</Context>

2.另外写了一个ConnectionManager类管理connection
public class ConnectionManager {
private static final Logger log = Logger.getLogger(ConnectionManager.class);
private static Connection connection = null;
private static DataSource ds = null;
private static Context initCtx = null;

static {
try {
initCtx = new InitialContext();
ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/dhlDB");
log.info("Read the database config file [context.xml] successful!");
} catch (NamingException e) {
log.error(e);
}
}
public static Connection getConn() {
try {
connection = ds.getConnection();
} catch (SQLException e) {
log.error(e);
}
// log.info("Create a connection successful!");
return connection;
}
public static Connection getConnection(boolean b) {
try {
connection = ds.getConnection();
connection.setAutoCommit(false);
} catch (SQLException e) {
log.error(e);
}
return connection;
}
private ConnectionManager() {
}
}

3.在JDBC操作中,每个类大致如下,获得Connection关闭Connection
public CountryBean getBean(String cty_Code) {
Connection conn = ConnectionManager.getConn();
LoggableStatement st = null;
ResultSet rs = null;
CountryBean bean = null;
try {
st = new LoggableStatement(conn, SELECT_BEAN);
st.setString(1, cty_Code);
rs = st.executeQuery();
while (rs.next()) {
bean = new CountryBean();
bean.setCty_Code(rs.getString("cty_Code"));
bean.setCty_S_Name(rs.getString("cty_S_Name"));
bean.setCty_Display_Name(rs.getString("cty_Display_Name"));
bean.setCty_Description(rs.getString("cty_Description"));
bean.setCty_Status(rs.getString("cty_Status"));
bean.setCty_Area_Code(rs.getString("cty_Area_Code"));
bean.setCty_Region_Code(rs.getString("cty_Region_Code"));
bean.setCty_Time_Zone(rs.getString("cty_Time_Zone"));
bean.setCty_Min_Topup(rs.getString("cty_Min_Topup"));
bean.setCty_Ccy(rs.getString("cty_Ccy"));
bean.setCty_Email_En(rs.getString("cty_Email_En"));
bean.setCty_Email_En_User(rs.getString("cty_Email_En_User"));
bean.setCty_Max_Topup(rs.getString("cty_Max_Topup"));
bean.setCty_Ship_Exp_Pre(rs.getString("cty_Ship_Exp_Pre"));
bean.setCty_Ship_Exp_Cash(rs.getString("cty_Ship_Exp_Cash"));
bean.setCty_Pre_Threshold(rs.getString("cty_Pre_Threshold"));
bean.setCty_Min_Balance(rs.getString("cty_Min_Balance"));
}
} catch (SQLException e) {
log.error(e);
} finally {
try {
rs.close();
st.close();
conn.close();
} catch (SQLException e) {
log.error(e);
}
}
return bean;
}


问题:
有时候服务器的LOG会有ERROR:SQLNestedException: Cannot get a connection, pool error timeout waiting for idle object.
请问:我上面JDBC代码有问题吗?获得一个Connection后需要close()吗?我不懂tomcat的连接池,还是说connection不需要close(),TOMCAT连接池自动会帮我们管理连接呢??还有context.xml配置有问题吗?
...全文
256 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
你的 ConnectionManager 代码有严重的问题,不能用!
xuechunzone 2011-07-22
  • 打赏
  • 举报
回复
支持
请问:mysql和oracle是怎样配置的呢?
  • 打赏
  • 举报
回复
获得一个Connection后需要close()吗?
--------------------------------------
必须得关!

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧