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

Author: baishuo <vc_java@hotmail.com>

Closes #3526 from baishuo/master-trycatch and squashes the following commits:

d446e14 [baishuo] correct the code style
b36bf96 [baishuo] correct the code style
ae0e447 [baishuo] add finally to avoid resource leak

(cherry picked from commit 69b6fed206565ecb0173d3757bcb5110422887c3)
Signed-off-by: Michael Armbrust <michael@databricks.com>