1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.jndi;
20
21 import java.lang.reflect.Method;
22 import java.sql.Connection;
23 import java.sql.Statement;
24
25 import javax.sql.DataSource;
26
27 import org.eclipse.jetty.util.component.Destroyable;
28 import org.eclipse.jetty.util.log.Log;
29 import org.eclipse.jetty.util.log.Logger;
30
31
32
33
34
35
36
37
38 public class DataSourceCloser implements Destroyable
39 {
40 private static final Logger LOG = Log.getLogger(DataSourceCloser.class);
41
42 final DataSource _datasource;
43 final String _shutdown;
44
45 public DataSourceCloser(DataSource datasource)
46 {
47 if (datasource==null)
48 throw new IllegalArgumentException();
49 _datasource=datasource;
50 _shutdown=null;
51 }
52
53 public DataSourceCloser(DataSource datasource,String shutdownSQL)
54 {
55 if (datasource==null)
56 throw new IllegalArgumentException();
57 _datasource=datasource;
58 _shutdown=shutdownSQL;
59 }
60
61 @Override
62 public void destroy()
63 {
64 try
65 {
66 if (_shutdown!=null)
67 {
68 LOG.info("Shutdown datasource {}",_datasource);
69 try (Connection connection = _datasource.getConnection();
70 Statement stmt = connection.createStatement())
71 {
72 stmt.executeUpdate(_shutdown);
73 }
74 }
75 }
76 catch (Exception e)
77 {
78 LOG.warn(e);
79 }
80
81 try
82 {
83 Method close = _datasource.getClass().getMethod("close", new Class[]{});
84 LOG.info("Close datasource {}",_datasource);
85 close.invoke(_datasource, new Object[]{});
86 }
87 catch (Exception e)
88 {
89 LOG.warn(e);
90 }
91 }
92 }