source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/sql/derby/DerbyDBWrapper.java@ 21303

Last change on this file since 21303 was 21303, checked in by xiao, 14 years ago

close db connections and statements after they are used

File size: 6.3 KB
Line 
1package org.greenstone.gsdl3.sql.derby;
2
3import org.greenstone.gsdl3.sql.*;
4
5
6import java.sql.Connection;
7import java.sql.ResultSet;
8import java.sql.ResultSetMetaData;
9import java.sql.SQLException;
10import java.sql.SQLWarning;
11import java.sql.Statement;
12
13import java.util.ArrayList;
14import java.util.HashMap;
15import java.util.Iterator;
16
17
18import org.greenstone.gsdl3.util.DBInfo;
19
20import org.apache.log4j.*;
21
22public class DerbyDBWrapper implements MetadataDBWrapper{
23 protected SQLServer sqlServer = null;
24 protected SQLStatements sqlState = null;
25 protected Connection connection = null;
26 protected Statement statement = null;
27 protected static Logger logger = Logger.getLogger(org.greenstone.gsdl3.sql.derby.DerbyDBWrapper.class.getName());
28
29
30 public DBInfo getInfo(String id){
31 if(sqlState == null) return null;
32 String dbInfoState = sqlState.getDBInfoStatement(id);
33 ArrayList result = executeQuery(dbInfoState);
34 if (result.size() == 0) return null;
35 DBInfo info = new DBInfo();
36 for(int i=0; i<result.size();i++){
37 HashMap arow = (HashMap)result.get(i);
38 Iterator ite = arow.keySet().iterator();
39 while(ite.hasNext()){
40 String key = (String)ite.next();
41 Object value = arow.get(key);
42 //logger.info(key + "=>" +value);
43 if (value == null || value.equals("")) continue;
44 info.addInfo(key.toLowerCase(),sqlState.undoDBSafe(value));
45 }
46 }
47
48 return info;
49 }
50
51 public void setSQLServer(SQLServer server){
52 sqlServer = server;
53 }
54
55 public void setSQLStatements(SQLStatements state){
56 sqlState = state;
57 }
58
59 public boolean openConnection(String databasepath){
60 connection = sqlServer.connect(databasepath);
61
62 if (connection == null) return false;
63
64 try{
65 connection.setAutoCommit(false);
66 statement = connection.createStatement();
67 }
68 catch(SQLException sqle){
69 logger.debug("Database Error occured when creating a statement object",sqle);
70 return false;
71 }
72 return true;
73 }
74
75 public boolean openAndCreateConnection(String databasepath){
76 connection = sqlServer.connectAndCreate(databasepath);
77
78 if (connection == null) {
79 logger.error("sql connection is null. database path="+databasepath);
80 return false;
81 }
82
83 try{
84 connection.setAutoCommit(true);
85 //by passing the two arguments, the ResultSet returned from executeQuery is updatable
86 statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
87 if (statement == null) {
88 logger.error("statement is null");
89 return false;
90 }
91
92 }
93 catch(SQLException sqle){
94 logger.debug("Database Error occured when creating a statement object",sqle);
95 return false;
96 }
97 return true;
98 }
99
100 public synchronized ArrayList executeQuery(String query_statement){
101 //the database hasn't been correct yet
102 ArrayList results = new ArrayList();
103 ResultSet rs = null;
104 try{
105
106 if (statement == null){
107 logger.info("the database hasn't been correct yet");
108 return new ArrayList();
109 }
110
111 rs = statement.executeQuery(query_statement);
112 ResultSetMetaData rsmd = rs.getMetaData();
113 int numOfColumns = rsmd.getColumnCount();
114 while(rs.next()){
115 HashMap arow = new HashMap();
116 for(int i = 1; i <= numOfColumns ; i++){
117 arow.put(rsmd.getColumnName(i).toLowerCase(), rs.getObject(i));
118 }
119 results.add(arow);
120 }
121
122 rs.close();
123
124 }
125 catch(SQLException sqle){
126 logger.debug("Database Error occured when executeQuery " + query_statement,sqle);
127 return results;
128 }
129 catch(Exception e){
130 logger.debug(e);
131 return null;
132 }
133 return results;
134 }
135 /**
136 * Same as executeQuery except the return type is ResultSet
137 * @param stat
138 * @return ResultSet of querying the statement 'stat'
139 */
140 public synchronized ResultSet queryResultSet(String stat){
141 ResultSet rs = null;
142 try{
143 if (statement == null){
144 logger.info("Null sql statement provided.");
145 return null;
146 }
147 logger.debug(stat);
148 rs = statement.executeQuery(stat);
149 logger.info("sql stat="+stat+ " result="+rs);
150 }
151 catch(SQLException sqle){
152 logger.info("Database Error occured when execute query " + stat, sqle);
153 return null;
154 }
155 catch(Exception e){
156 logger.info("Exception="+e);
157 return null;
158 }
159 logger.debug(" result="+rs);
160 return rs;
161 }
162 /**
163 * Used by create (table)
164 */
165 public synchronized boolean execute(String stat) {
166 boolean rs;
167 try{
168 if (statement == null){
169 logger.info("statement is null.");
170 return false;
171 }
172
173 rs = statement.execute(stat);
174 statement.close();
175 // connection.commit();
176 }
177 catch(SQLException sqle){
178 logger.debug("Database Error occured when execute query " + stat, sqle);
179 return false;
180 }
181 catch(Exception e){
182 logger.debug(e);
183 return false;
184 }
185 return rs;
186 }
187 /**
188 * Used by insert, update, and delete
189 */
190 public synchronized boolean executeUpdate(String stat) {
191 int rs;
192 try{
193 if (statement == null){
194 logger.info("statement is null.");
195 return false;
196 }
197
198 //either the row count of INSERT, UPDATE OR DELETE statement, or 0 if the statement returns nothing
199 rs = statement.executeUpdate(stat);
200 statement.close();
201// connection.commit();
202 logger.debug("sql stat="+stat+ " result="+rs);
203 }
204 catch(SQLException sqle){
205 logger.debug("Database Error occured when execute query " + stat, sqle);
206 return false;
207 }
208 catch(Exception e){
209 logger.debug("Exception="+e);
210 return false;
211 }
212 logger.debug(" result="+rs);
213 return (rs==-1)? false : true;
214 }
215 /**
216 * Used by checking the existence of table
217 * Same as the method 'execute' except throws SQLException
218 */
219 public void check4Table(String stat) throws SQLException {
220 statement.executeQuery(stat);
221 statement.close();
222 }
223
224 public void closeConnection(String databasepath){
225 try{
226 statement.close();
227 connection.close();
228 sqlServer.disconnect(databasepath);
229 }
230 catch(SQLException sqle){
231 logger.debug("Database Error occured when close connection " + databasepath, sqle);
232 }
233 }
234
235 }
Note: See TracBrowser for help on using the repository browser.