[SPARK-4663][sql]add finally to avoid resource leak