summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorShireesh Anjal <shireesh@gluster.com>2011-08-18 21:31:33 +0530
committerShireesh Anjal <shireesh@gluster.com>2011-08-18 21:31:33 +0530
commit29f7b12e64f79e1d41065605b5961b82768ab93c (patch)
treee5eb33f909d139333b35dc6072a2ae6fe52b5010 /src
parent3ea60f945ce1d94b1e2598e96254665e56b5e626 (diff)
Utility class for DB related functionality
Diffstat (limited to 'src')
-rw-r--r--src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/DBUtil.java85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/DBUtil.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/DBUtil.java
new file mode 100644
index 00000000..e224b4cb
--- /dev/null
+++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/DBUtil.java
@@ -0,0 +1,85 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com>
+ * This file is part of Gluster Management Console.
+ *
+ * Gluster Management Console is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License as published
+ * by the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Gluster Management Console is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *******************************************************************************/
+package com.gluster.storage.management.gateway.utils;
+
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import org.apache.derby.jdbc.EmbeddedDriver;
+import org.apache.log4j.Logger;
+
+import com.gluster.storage.management.core.constants.CoreConstants;
+
+/**
+ *
+ */
+public class DBUtil {
+ private static final Logger logger = Logger.getLogger(DBUtil.class);
+ public static void shutdownDerby() {
+ try {
+ // the shutdown=true attribute shuts down Derby
+ DriverManager.getConnection("jdbc:derby:;shutdown=true");
+
+ // To shut down a specific database only, but keep the
+ // engine running (for example for connecting to other
+ // databases), specify a database in the connection URL:
+ //DriverManager.getConnection("jdbc:derby:" + dbName + ";shutdown=true");
+ } catch (Exception e) {
+ if(e instanceof SQLException) {
+ SQLException se = (SQLException) e;
+ if (((se.getErrorCode() == 50000) && ("XJ015".equals(se.getSQLState())))) {
+ // we got the expected exception
+ logger.info("Derby shut down normally");
+ // Note that for single database shutdown, the expected
+ // SQL state is "08006", and the error code is 45000.
+ } else {
+ // if the error code or SQLState is different, we have
+ // an unexpected exception (shutdown failed)
+ logger.error("Derby did not shut down normally!" + inspectSQLException(se), se);
+ }
+ } else {
+ logger.error("Derby did not shut down normally! [" + e.getMessage() + "]", e);
+ }
+ }
+ // force garbage collection to unload the EmbeddedDriver
+ // so Derby can be restarted
+ System.gc();
+ }
+
+ /**
+ * Extracts details of an SQLException chain to <code>String</code>.
+ * Details included are SQL State, Error code, Exception message.
+ *
+ * @param e the SQLException from which to print details.
+ */
+ private static String inspectSQLException(SQLException e)
+ {
+ // Unwraps the entire exception chain to unveil the real cause of the
+ // Exception.
+ String errMsg = "";
+ while (e != null)
+ {
+ errMsg += "\n----- SQLException -----" + CoreConstants.NEWLINE + " SQL State: " + e.getSQLState()
+ + CoreConstants.NEWLINE + " Error Code: " + e.getErrorCode() + CoreConstants.NEWLINE
+ + " Message: " + e.getMessage();
+ e = e.getNextException();
+ }
+ return errMsg;
+ }
+}