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