source: branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/gs3build/database/SQLConnection.java@ 9858

Last change on this file since 9858 was 9858, checked in by kjdon, 19 years ago

OK, changed my mind about making SQLConnection kill off the previous statement.
To make it more transparent what is happening, you now have to create a Statement (connection.createStatement()), then use the Statement to execute the query. This means that the thing doing the query owns the Statement, and can kill it off when finished with it, and nothing else can kill it off unexpectedly. The previous way this was all implemented meant that there was a large memory leak, and some functionality actually relied on this. A newer version of the mysql connector/J has fixed the bug where the statement wasn't closed on garbage collection, but it still seems better to close it explicitly.
Hopefully I have got it all back to working as well as it was bfore, and haven't introduced any bugs :-)

  • Property svn:keywords set to Author Date Id Revision
File size: 1.7 KB
Line 
1package org.greenstone.gsdl3.gs3build.database;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.SQLException;
6import java.sql.Statement;
7import java.sql.ResultSet;
8
9public class SQLConnection
10{
11 protected Connection connection;
12 protected String database;
13
14 public SQLConnection(java.sql.Connection connection, String database)
15 {
16 this.connection = connection;
17 this.database = database;
18 }
19
20 protected void finalize() {
21 if (this.connection!=null) {
22 try {
23 this.connection.close();
24 } catch (Exception e) {}
25 }
26 this.connection = null;
27 }
28
29 public void close() {
30 finalize();
31 }
32
33 public Statement createStatement() throws SQLException
34 {
35 return this.connection.createStatement();
36 }
37
38 public boolean connectToDatabase(String database) {
39 if (this.connection != null) {
40 try {
41 this.connection.close();
42 } catch (Exception e) {}
43 }
44 this.connection = GS3SQLConnectionFactory.getConnection(database);
45 if (this.connection == null) {
46 return false;
47 }
48 this.database = database;
49 return true;
50 }
51
52 public boolean dropDatabase(String database) {
53 try {
54 Statement statement = this.connection.createStatement();
55 statement.execute("DROP DATABASE "+database+";");
56 statement.close();
57 }
58 catch (SQLException ex){
59 System.err.println(ex);
60 return false;
61 }
62 return true;
63
64 }
65
66 public boolean createDatabase(String database) {
67 try {
68 String command = "CREATE DATABASE " + database;
69 Statement statement = this.connection.createStatement();
70 statement.execute(command);
71 statement.close();
72 } catch (Exception e) {
73 System.err.println(e);
74 return false;
75 }
76
77 return true;
78 }
79
80}
Note: See TracBrowser for help on using the repository browser.