DBA经常会遇到因为应用没有断开会话导致数据库中的非活动会话太多内存被占满的情况,需要手工KILL或者定期使用脚本KILL会话来释放内存,甚至还有定期重启。
现在介绍一种使用Oracle资源管理器Resource Manager来完成的这一项工作的方式。资源管理器通过限制max_idle_time来达到此效果。
任何空闲时间超过300秒的会话都将被自动终止:
begin
dbms_resource_manager.create_pending_area;
dbms_resource_manager.create_plan(
plan => 'idle_mgmt_plan',
comment => 'Detects and kills idle sessions');
dbms_resource_manager.create_plan_directive(
plan => 'idle_mgmt_plan',
group_or_subplan => 'other_groups',
max_idle_time => 300,
comment => 'Limits idle time for all sessions to 300 seconds');
dbms_resource_manager.submit_pending_area;
end;
/
alter system set resource_manager_plan = idle_mgmt_plan;
也可以根据不同的需求,创建不同max_idle_time的组group_or_subplan。
还可以使用指令MAX_IDLE_BLOCKER_TIME,配置仅在会话阻止另一个会话时强制执行的会话的最大空闲时间。
监控
查看当前的资源管理计划
select name from v$rsrc_plan;
查看每个计划组的限制时长
select d.group_or_subplan, d.max_idle_time, d.max_idle_blocker_time from dba_rsrc_plan_directives d, v$rsrc_plan p where d.plan = p.name and (d.max_idle_time is not null or d.max_idle_blocker_time is not null) and d.status is null;
此查询显示到目前为止已被杀死的空闲会话数。
select name, idle_sessions_killed, idle_blkr_sessions_killed from v$rsrc_consumer_group;
官方建议最好在11g及以上版本使用此功能,否则有可能遇到BUG。
参考:
Using Resource Manager to Detect and Kill Idle Sessions (Doc ID 1557657.1)