连接远程mysql时间很长,远程数据库连接打开很长时间

I am executing some crud's on mssql server 2008 database by connecting remotely. I have business logic in Java with tight code for connection, statement and result set close() in finally block.

My doubt is, even when I finish the job the connection port are still open and it takes roughly 20 min to close all connections. I can view the open connection using netstat -a command.

I need to quickly close the connections since each operation opens/close connection several times. Hence I see large number of connections open. This is very insecure and costly to keep so many unwanted connections for long time which the business logic has already closed.

I am using the latest jtds.jar file. Any hint or suggestion will be appreciated.

Thanks,

Akshay

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

// Do stuff

...

} catch (SQLException ex) {

// Exception handling stuff

...

} finally {

if (rs != null) {

try {

rs.close();

} catch (SQLException e) { /* ignored */}

}

if (ps != null) {

try {

ps.close();

} catch (SQLException e) { /* ignored */}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) { /* ignored */}

}

}

解决方案

Sorry guys and Thanks for taking out time to answer my question.

The connection close was a framework call which was not closing properly. I didn't had access to it so I thought that was not the issue. Thank you once again for your time and guidance.

MySQL数据库访问变慢可能是由多种原因导致的,这些原因可以分为性能瓶颈、配置不当、索引问题、网络延迟、硬件限制或并发问题等几个方面。以下是一些常见的原因和解决策略: 1. **查询优化问题**:SQL查询可能过于复杂或者没有有效的索引,导致全表扫描。检查是否存在不必要的JOIN操作,或者使用EXPLAIN分析查询执行计划。 2. **索引问题**:索引是加快查询速度的关键,如果关键字段没有合适的索引,查询将变得非常慢。确保对经常用于WHERE、JOIN和ORDER BY的列创建了适当的索引。 3. **内存限制**:如果内存不足,MySQL可能会进行大量的磁盘I/O,降低性能。检查 Innodb_buffer_pool_size 配置是否足够,以及缓存命中率。 4. **硬件性能**:服务器的CPU、硬盘I/O或者内存速度过慢也可能影响数据库性能。考虑升级硬件资源。 5. **网络延迟**:如果数据存储在远程服务器,网络延迟可能导致通信时间过长。检查网络连接质量并优化网络配置。 6. **并发问题**:大量并发请求可能会导致锁竞争,特别是InnoDB引擎。优化事务处理,减少死锁和锁定时间。 7. **数据库配置**:MySQL的系统参数设置不合理,比如max_connections限制过高或过低,也会影响性能。适当调整参数。 8. **数据库碎片化**:随着数据的增长,表可能会变得碎片化,影响查询效率。定期执行OPTIMIZE TABLE或ANALYZE TABLE来维护表的完整性。 9. **事务隔离级别**:高事务隔离级别可能导致更多的锁定,影响并发性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值