1 | package org.greenstone.gsdl3.sql.derby;
|
---|
2 |
|
---|
3 |
|
---|
4 | import org.apache.log4j.*;
|
---|
5 | import org.greenstone.gsdl3.sql.*;
|
---|
6 | import java.sql.Connection;
|
---|
7 | import java.sql.DriverManager;
|
---|
8 | import java.sql.SQLException;
|
---|
9 |
|
---|
10 | import org.greenstone.util.GlobalProperties;
|
---|
11 |
|
---|
12 | public class DerbySQLServer implements SQLServer{
|
---|
13 |
|
---|
14 | static final String PORT = GlobalProperties.getProperty("derby.server.port");//, "1527");
|
---|
15 | static final String PROTOCOL = "jdbc:derby://localhost:"+PORT+"/"; //"jdbc:derby:";
|
---|
16 | static final String DRIVER = "org.apache.derby.jdbc.ClientDriver"; //"org.apache.derby.jdbc.EmbeddedDriver";
|
---|
17 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.sql.derby.DerbySQLServer.class.getName());
|
---|
18 |
|
---|
19 | public DerbySQLServer(){
|
---|
20 | try{
|
---|
21 | Class.forName(DRIVER).newInstance();
|
---|
22 | }
|
---|
23 | catch(Exception e){
|
---|
24 | logger.error("Couldn't find derby driver: "+DRIVER,e);
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | public Connection connect(String databasePath){
|
---|
29 | try{
|
---|
30 | String protocol_str = PROTOCOL + databasePath;
|
---|
31 | Connection connection = DriverManager.getConnection(protocol_str);
|
---|
32 | return connection;
|
---|
33 | }
|
---|
34 | catch(Exception e){
|
---|
35 | logger.info("Connect to database "+databasePath + " failed!");
|
---|
36 | }
|
---|
37 |
|
---|
38 | return null;
|
---|
39 |
|
---|
40 | }
|
---|
41 |
|
---|
42 | public Connection connectAndCreate(String databasePath){
|
---|
43 | try{
|
---|
44 | String protocol_str = PROTOCOL + databasePath + ";create=true";
|
---|
45 | Connection connection = DriverManager.getConnection(protocol_str);
|
---|
46 | return connection;
|
---|
47 | }
|
---|
48 | catch(Exception e){
|
---|
49 | logger.error("Connect to database "+databasePath + " failed!",e);
|
---|
50 |
|
---|
51 | }
|
---|
52 | return null;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public boolean disconnect(String databasePath){
|
---|
56 | try{
|
---|
57 | // Only shutdown if using embedded derby,
|
---|
58 | // not if it's a networked derby server, which is what we now use
|
---|
59 |
|
---|
60 | if(DRIVER.equals("org.apache.derby.jdbc.EmbeddedDriver")) {
|
---|
61 | String protocol_str = PROTOCOL + databasePath + ";shutdown=true";
|
---|
62 | DriverManager.getConnection(protocol_str);
|
---|
63 | }
|
---|
64 | }catch (SQLException se){
|
---|
65 | String theError = (se).getSQLState();
|
---|
66 | if (!theError.equals("08006")){
|
---|
67 | // logger.error("Database "+databasePath + " couldn't be shut down properly!",se);
|
---|
68 | }
|
---|
69 | else{
|
---|
70 | return true;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 |
|
---|
79 | }
|
---|