1 package fr.ifremer.tutti.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import fr.ifremer.tutti.TuttiConfiguration;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.hibernate.cfg.Environment;
31
32 import java.io.File;
33 import java.sql.Connection;
34 import java.sql.DriverManager;
35 import java.sql.PreparedStatement;
36 import java.sql.ResultSet;
37 import java.sql.SQLException;
38 import java.sql.Statement;
39 import java.util.Properties;
40
41
42
43
44
45
46
47 public class Jdbcs {
48
49
50 private static final Log log = LogFactory.getLog(Jdbcs.class);
51
52 public static void closeSilently(Statement statement) {
53 try {
54 if (statement != null && !statement.isClosed()) {
55
56 statement.close();
57 }
58 } catch (AbstractMethodError e) {
59 if (log.isDebugEnabled()) {
60 log.debug("Fix this linkage error, damned hsqlsb 1.8.0.7:(");
61 }
62 } catch (IllegalAccessError e) {
63 if (log.isDebugEnabled()) {
64 log.debug("Fix this IllegalAccessError error, damned hsqlsb 1.8.0.7:(");
65 }
66 } catch (Exception e) {
67 if (log.isErrorEnabled()) {
68 log.error("Could not close statement, but do not care", e);
69 }
70 }
71 }
72
73 public static void closeSilently(ResultSet statement) {
74 try {
75 if (statement != null && !statement.isClosed()) {
76
77 statement.close();
78 }
79 } catch (AbstractMethodError e) {
80 if (log.isDebugEnabled()) {
81 log.debug("Fix this linkage error, damned hsqlsb 1.8.0.7:(");
82 }
83 } catch (IllegalAccessError e) {
84 if (log.isDebugEnabled()) {
85 log.debug("Fix this IllegalAccessError error, damned hsqlsb 1.8.0.7:(");
86 }
87 } catch (Exception e) {
88 if (log.isErrorEnabled()) {
89 log.error("Could not close statement, but do not care", e);
90 }
91 }
92 }
93
94 public static Connection createConnection(Properties connectionProperties) throws SQLException {
95 return createConnection(
96 connectionProperties.getProperty(Environment.URL),
97 connectionProperties.getProperty(Environment.USER),
98 connectionProperties.getProperty(Environment.PASS)
99 );
100 }
101
102 public static String getUrl(Properties connectionProperties) {
103 return connectionProperties.getProperty(Environment.URL);
104 }
105
106 public static Connection createConnection(String jdbcUrl,
107 String user,
108 String password) throws SQLException {
109 Connection connection = DriverManager.getConnection(jdbcUrl,
110 user,
111 password);
112 connection.setAutoCommit(false);
113 return connection;
114 }
115
116 public static void fillConnectionProperties(Properties p,
117 String url,
118 String username,
119 String password) {
120 p.put(Environment.URL, url);
121 p.put(Environment.USER, username);
122 p.put(Environment.PASS, password);
123 }
124
125 public static String getJdbcUrl(File directory, String dbName) {
126 return "jdbc:hsqldb:file:" + directory.getAbsolutePath() + "/" + dbName;
127 }
128
129 public static void shutdown(TuttiConfiguration configuration) throws SQLException {
130
131 String jdbcUrl = configuration.getJdbcUrl();
132 String jdbcUsername = configuration.getJdbcUsername();
133 String jdbcPassword = configuration.getJdbcPassword();
134 try (Connection connection = createConnection(jdbcUrl, jdbcUsername, jdbcPassword)) {
135
136 try (PreparedStatement pStmt = connection.prepareStatement("SHUTDOWN")) {
137 pStmt.execute();
138 }
139 }
140
141 }
142
143 public static void shutdownCompact(TuttiConfiguration configuration) throws SQLException {
144
145 String jdbcUrl = configuration.getJdbcUrl();
146 String jdbcUsername = configuration.getJdbcUsername();
147 String jdbcPassword = configuration.getJdbcPassword();
148 try (Connection connection = createConnection(jdbcUrl, jdbcUsername, jdbcPassword)) {
149
150 try (PreparedStatement pStmt = connection.prepareStatement("SHUTDOWN COMPACT")) {
151 pStmt.execute();
152 }
153 }
154
155 }
156
157 }