---------------这是我的连接类---------
有的时候执行到query()方法的
//编译sql语句
pst = connection.prepareStatement(sql);
这一句时会阻塞住,起码一两分钟,请问我这样写有问题吗?得怎么修改
public final class DbTool
{
/**
* 初始化类
*/
private static DbTool tool = new DbTool();
/**
* 连接池
*/
private static DataSource pool;
/**
* 记录日志
*/
private static UCDLog logger = LogManager.getLog(Contants.COMMON_LOG);
/**
* 单例
*/
private DbTool()
{
}
/**
* 获取单实例类
* @return DbTool
*/
public static DbTool getInstance()
{
return tool;
}
/**
* 初始化连接池
*/
public static void init()
{
//记录进入日志
if (logger.isInfoEnabled())
{
logger.info("Entry the DbTool:initialization DataSource");
}
Context context = null;
try
{
context = (Context) new InitialContext().lookup("java:comp/env");
//获取连接池
pool = (DataSource) context.lookup("jdbc/mysqlpool");
}
catch (NamingException e)
{
logger.error("DataSource acquire failed " + e.toString(), e);
}
//记录退出日志
if (logger.isInfoEnabled())
{
logger.info("Exit the DbTool:initialization DataSource");
}
}
/**
* 获取连接池
* @return DataSource
*/
private static DataSource getPool()
{
//记录进入日志
if (logger.isInfoEnabled())
{
logger.info("Enter the DbTool:getPool");
}
if (null == pool)
{
logger.warn("the pool is null");
//如果为空就再初始化一次
init();
}
else
{
return pool;
}
//如果不为空则返回pool
if (null != pool)
{
//记录退出日志
if (logger.isInfoEnabled())
{
logger.info("Exit the DbTool:getPool");
}
return pool;
}
else
{
logger.error("The database pool is null!");
}
return null;
}
/**
* 获取连接
* @return Connection
*/
public static synchronized Connection getConnection()
{
//记录进入日志
if (logger.isInfoEnabled())
{
logger.info("Entry the DbTool:getConnection");
}
Connection connection = null;
if (null == getPool())
{
logger.error("The database pool is null!");
//如果连接池为空则返回null
return null;
}
try
{
//从连接池中得到连接
connection = pool.getConnection();
}
catch (SQLException e)
{
logger.error("Connection acquire failed" + e.toString());
return null;
}
//记录退出日志
if (logger.isInfoEnabled())
{
logger.info("Exit the DbTool:getConnection:" + connection);
}
return connection;
}
/**
* 执行查询操作
* @param sql sql语句
* @param param 参数
* @return ArrayList ArrayList
*/
@SuppressWarnings("unchecked")
public List query(final String sql, Object... param)
{
//记录进入日志
if (logger.isInfoEnabled())
{
logger.info("Entry the DbTool:query,The sql is " + sql);
}
Connection connection = null;
PreparedStatement pst = null;
ResultSet result = null;
//获取结果
List list = new ArrayList();
try
{
//获取连接
connection = getConnection();
if (null == connection)
{
logger.warn("the connection is null");
return list;
}
if (logger.isDebugEnabled())
{
logger.debug("the connection is :" + connection);
}
//编译sql语句
pst = connection.prepareStatement(sql);
if (logger.isDebugEnabled())
{
logger.debug("the pst is :" + pst);
}
pst = addParam(pst, param);
//返回查询结果
result = pst.executeQuery();
Object[] info = null;
while (result.next())
{
info = new Object[result.getMetaData().getColumnCount()];
//把每行数据封装成一个数组
for (int i = 0; i < info.length; i++)
{
info[i] = result.getObject(i + 1);
if (null == info[i])
{
//如果为null则转换为""
info[i] = "";
}
}
//添加到list中
list.add(info);
}
}
catch (SQLException e)
{
logger.error("Error occured while executing sql:");
logger.error(sql);
logger.error(e.getMessage(), e);
}
finally
{
//关闭连接
closeAllConnection(result, pst, connection, null);
}
//记录退出日志
if (logger.isInfoEnabled())
{
logger.info("exit the DbTool:query");
}
return list;
} /**
* 添加参数
* @param pst PreparedStatement
* @param param 参数列表
* @return PreparedStatement
* @throws SQLException
*/
private PreparedStatement addParam(PreparedStatement pst, Object... param)
throws SQLException
{
if (param.length > 0)
{
//添加参数
for (int i = 1; i <= param.length; i++)
{
if (logger.isDebugEnabled())
{
logger.debug("param" + i + Contants.COLON
+ param[i - 1]);
}
//如果为null则转换为空,否则会跑nullException异常
if (null == param[i - 1])
{
param[i - 1] = "";
}
pst.setString(i, param[i - 1].toString());
}
}
return pst;
}
}
有的时候执行到query()方法的
//编译sql语句
pst = connection.prepareStatement(sql);
这一句时会阻塞住,起码一两分钟,请问我这样写有问题吗?得怎么修改
public final class DbTool
{
/**
* 初始化类
*/
private static DbTool tool = new DbTool();
/**
* 连接池
*/
private static DataSource pool;
/**
* 记录日志
*/
private static UCDLog logger = LogManager.getLog(Contants.COMMON_LOG);
/**
* 单例
*/
private DbTool()
{
}
/**
* 获取单实例类
* @return DbTool
*/
public static DbTool getInstance()
{
return tool;
}
/**
* 初始化连接池
*/
public static void init()
{
//记录进入日志
if (logger.isInfoEnabled())
{
logger.info("Entry the DbTool:initialization DataSource");
}
Context context = null;
try
{
context = (Context) new InitialContext().lookup("java:comp/env");
//获取连接池
pool = (DataSource) context.lookup("jdbc/mysqlpool");
}
catch (NamingException e)
{
logger.error("DataSource acquire failed " + e.toString(), e);
}
//记录退出日志
if (logger.isInfoEnabled())
{
logger.info("Exit the DbTool:initialization DataSource");
}
}
/**
* 获取连接池
* @return DataSource
*/
private static DataSource getPool()
{
//记录进入日志
if (logger.isInfoEnabled())
{
logger.info("Enter the DbTool:getPool");
}
if (null == pool)
{
logger.warn("the pool is null");
//如果为空就再初始化一次
init();
}
else
{
return pool;
}
//如果不为空则返回pool
if (null != pool)
{
//记录退出日志
if (logger.isInfoEnabled())
{
logger.info("Exit the DbTool:getPool");
}
return pool;
}
else
{
logger.error("The database pool is null!");
}
return null;
}
/**
* 获取连接
* @return Connection
*/
public static synchronized Connection getConnection()
{
//记录进入日志
if (logger.isInfoEnabled())
{
logger.info("Entry the DbTool:getConnection");
}
Connection connection = null;
if (null == getPool())
{
logger.error("The database pool is null!");
//如果连接池为空则返回null
return null;
}
try
{
//从连接池中得到连接
connection = pool.getConnection();
}
catch (SQLException e)
{
logger.error("Connection acquire failed" + e.toString());
return null;
}
//记录退出日志
if (logger.isInfoEnabled())
{
logger.info("Exit the DbTool:getConnection:" + connection);
}
return connection;
}
/**
* 执行查询操作
* @param sql sql语句
* @param param 参数
* @return ArrayList ArrayList
*/
@SuppressWarnings("unchecked")
public List query(final String sql, Object... param)
{
//记录进入日志
if (logger.isInfoEnabled())
{
logger.info("Entry the DbTool:query,The sql is " + sql);
}
Connection connection = null;
PreparedStatement pst = null;
ResultSet result = null;
//获取结果
List list = new ArrayList();
try
{
//获取连接
connection = getConnection();
if (null == connection)
{
logger.warn("the connection is null");
return list;
}
if (logger.isDebugEnabled())
{
logger.debug("the connection is :" + connection);
}
//编译sql语句
pst = connection.prepareStatement(sql);
if (logger.isDebugEnabled())
{
logger.debug("the pst is :" + pst);
}
pst = addParam(pst, param);
//返回查询结果
result = pst.executeQuery();
Object[] info = null;
while (result.next())
{
info = new Object[result.getMetaData().getColumnCount()];
//把每行数据封装成一个数组
for (int i = 0; i < info.length; i++)
{
info[i] = result.getObject(i + 1);
if (null == info[i])
{
//如果为null则转换为""
info[i] = "";
}
}
//添加到list中
list.add(info);
}
}
catch (SQLException e)
{
logger.error("Error occured while executing sql:");
logger.error(sql);
logger.error(e.getMessage(), e);
}
finally
{
//关闭连接
closeAllConnection(result, pst, connection, null);
}
//记录退出日志
if (logger.isInfoEnabled())
{
logger.info("exit the DbTool:query");
}
return list;
} /**
* 添加参数
* @param pst PreparedStatement
* @param param 参数列表
* @return PreparedStatement
* @throws SQLException
*/
private PreparedStatement addParam(PreparedStatement pst, Object... param)
throws SQLException
{
if (param.length > 0)
{
//添加参数
for (int i = 1; i <= param.length; i++)
{
if (logger.isDebugEnabled())
{
logger.debug("param" + i + Contants.COLON
+ param[i - 1]);
}
//如果为null则转换为空,否则会跑nullException异常
if (null == param[i - 1])
{
param[i - 1] = "";
}
pst.setString(i, param[i - 1].toString());
}
}
return pst;
}
}
解决方案 »
- 急!!如何实现自动计算总分的方法?
- 求助:<rich:editor> 运用!!!!!!!!!!
- 谁给我解释下啊
- ‖‖‖tomcat无法在lomboz-eclipse中启动‖‖‖
- 为什么我的Tomcat占有了600M内存,是因为程序使用了缓存吗?任务管理器里面怎么会有两个Apache.exe进程?
- struts关于循环表单 验证回显示的问题(请高手指点)
- 一个关于strutsr的奇怪问题
- Weblogic中如何使Class自动更新?
- 菜鸟问题:如何修改entity bean表中的纪录
- Hibernate 执行十万级数据批量操作时遇到的错误 求大神!
- struts2 令牌问题
- JAX RS 如何输出图片
//编译sql语句
pst = connection.prepareStatement(sql);
这一句时会阻塞住,起码一两分钟
那就是可以运行了?阻塞的话,看看是不是数据库的问题,和你怎么写没有关系........