[15081] | 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 |
|
---|
[29903] | 10 | import org.greenstone.util.GlobalProperties;
|
---|
| 11 |
|
---|
[15081] | 12 | public class DerbySQLServer implements SQLServer{
|
---|
[29903] | 13 |
|
---|
| 14 | static final String PORT = GlobalProperties.getProperty("derby.server.port");//, "1527");
|
---|
[29977] | 15 | static final String DERBYSERVER = GlobalProperties.getProperty("derby.server");//, "localhost");
|
---|
| 16 | static final String PROTOCOL = "jdbc:derby://"+DERBYSERVER+":"+PORT+"/"; // "jdbc:derby://localhost:1527";
|
---|
[29903] | 17 | static final String DRIVER = "org.apache.derby.jdbc.ClientDriver"; //"org.apache.derby.jdbc.EmbeddedDriver";
|
---|
[15081] | 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 | try{
|
---|
[29903] | 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 | String protocol_str = PROTOCOL + databasePath + ";shutdown=true";
|
---|
| 63 | DriverManager.getConnection(protocol_str);
|
---|
| 64 | }
|
---|
[15081] | 65 | }catch (SQLException se){
|
---|
| 66 | String theError = (se).getSQLState();
|
---|
| 67 | if (!theError.equals("08006")){
|
---|
[20009] | 68 | // logger.error("Database "+databasePath + " couldn't be shut down properly!",se);
|
---|
[15081] | 69 | }
|
---|
| 70 | else{
|
---|
| 71 | return true;
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | return false;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | }
|
---|