【发布时间】:2015-06-23 20:44:04
【问题描述】:
我有一堆 MySQL 查询,它们使用临时表将复杂/昂贵的查询分成小块。
create temporary table product_stats (
product_id int
,count_vendors int
,count_categories int
,...
);
-- Populate initial values.
insert into product_stats(product_id) select product_id from product;
-- Incrementally collect stats info.
update product_stats ... join vendor ... set count_vendors = count(vendor_id);
update product_stats ... join category... set count_categories = count(category_id);
....
-- Consume the resulting temporary table.
select * from product_stats;
问题是,当我使用连接池时,即使我关闭了java.sql.Connection,这些表也不会被清除。
我可以在执行所需的查询之前一一手动删除它们(drop temporary table x;),但这可能会发生错误。
有没有办法(JDBC/MySQL、API/配置)在不关闭数据库连接的情况下重置当前会话中创建的所有临时表(如您所知,我不是指java.sql.Connection.close()),这样我仍然可以使用提供连接池的优势吗?
已编辑:
似乎只有从 MySQL 5.7.3 版开始,他们才开始实施“重置连接”功能。 (发布说明:https://dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-3.html)但是,我暂时不会使用它,因为 5.7 版仍处于开发版本中。
【问题讨论】:
-
要么让 mysql 通过关闭连接进行自己的清理,要么清理你自己创建的每个变量/表。 mysql 中没有“将环境重置为最初的状态”功能。
-
很好地发现了 MySQL 5.7.3 中引入的
mysql_reset_connection函数。
标签: java mysql connection-pooling user-variables