1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package olr.om;
18
19 import java.sql.Connection;
20 import java.sql.ResultSet;
21 import java.sql.SQLException;
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.log4j.Logger;
29 import org.apache.torque.Torque;
30 import org.apache.torque.util.Transaction;
31
32 /***
33 * @version $Id: DBUtils.java,v 1.11 2004/08/04 12:11:18 roku Exp $
34 */
35 public final class DBUtils {
36
37 private final static Map ACTIVE_CONNECTIONS_MAP = new HashMap();
38
39 public static String escapeCharacters(String original) {
40 String specials = "'";
41 String copy = "";
42 for (int i = 0; i < original.length(); i++) {
43 if (specials.indexOf(original.charAt(i)) >= 0)
44 copy = String.valueOf(copy) + String.valueOf(original.charAt(i));
45 copy = String.valueOf(copy) + String.valueOf(original.charAt(i));
46 }
47
48 return copy;
49 }
50
51 public static int[] getIds(final ResultSet resultSet, final String columnName)
52 throws SQLException {
53
54 final List ids = new ArrayList(8192);
55 resultSet.beforeFirst();
56
57 while (resultSet.next())
58 ids.add(new Integer(resultSet.getInt(columnName)));
59
60 final int[] returnIds = new int[ids.size()];
61 for (int i = 0; i < ids.size(); i++)
62 returnIds[i] = ((Integer) ids.get(i)).intValue();
63
64 resultSet.beforeFirst();
65 return returnIds;
66 }
67
68 public static String idsToInList(final int[] ids) {
69 if (ids.length == 0)
70 return "()";
71
72 final StringBuffer inlist = new StringBuffer(ids.length * 5);
73 inlist.append("(");
74
75 for (int i = 0; i < ids.length; i++)
76 inlist.append(ids[i] + ",");
77
78 return inlist.deleteCharAt(inlist.length() - 1).append(")").toString();
79 }
80
81 public static Connection getConnection() throws Exception {
82 final Connection connection = Transaction.begin(Torque.getDefaultDB());
83 ACTIVE_CONNECTIONS_MAP.put(connection, "" + (ACTIVE_CONNECTIONS_MAP.size() + 1));
84 Logger.getLogger(DBUtils.class).debug(
85 "Allocated connection " + ACTIVE_CONNECTIONS_MAP.get(connection));
86 OmContext.setConnection(connection);
87 return connection;
88 }
89
90 /***
91 * Commits all changes and releases a database connection.
92 *
93 * @param connection The database connection to release.
94 */
95 public static void releaseConnection(Connection connection) {
96 try {
97 assert (connection != null && !connection.isClosed());
98 Logger.getLogger(DBUtils.class).debug(
99 "Freeing connection " + ACTIVE_CONNECTIONS_MAP.get(connection));
100 Transaction.commit(connection);
101 ACTIVE_CONNECTIONS_MAP.remove(connection);
102 } catch (Exception e) {
103 final String msg = "Error closing or committing db connection.";
104 Logger.getLogger(DBUtils.class).error(msg + e);
105 throw new RuntimeException(msg, e);
106 }
107 }
108
109 /***
110 * Rollback all queries done on this connection. Also releases this connection.
111 *
112 * @param connection The connection to rollback.
113 * @param exception The Exception, which causes this rollback.
114 */
115 public static void releaseConnection(Connection connection, Exception exception) {
116 try {
117 assert (connection != null && !connection.isClosed());
118 Logger.getLogger(DBUtils.class).debug(
119 "Freeing connection " + ACTIVE_CONNECTIONS_MAP.get(connection));
120 Transaction.rollback(connection);
121 Logger.getLogger(DBUtils.class).debug("Rolling back connection due to exception.",
122 exception);
123 ACTIVE_CONNECTIONS_MAP.remove(connection);
124 } catch (Exception e) {
125 final String msg = "Error rollin' back db connection.";
126 Logger.getLogger(DBUtils.class).error(msg, e);
127 throw new RuntimeException(msg, e);
128 }
129 }
130
131 public static long getInstanceId() {
132 try {
133 Thread.sleep(10L);
134 } catch (Exception exception) {
135 }
136 Date now = new Date();
137 return now.getTime();
138 }
139 }