Sep 26, 2005

[Derby] Derby を server モードで起動

先日は Derby を embedded モードで起動したので、今回は server モードを試してみた。 server モードの場合も embedded モードとほとんど同一のコードで起動できた。 意外と簡単。 クライアント側も Driver と URL を変更したら何事も無く接続できた。

Derby の実行環境構築

Derby を Server モードで起動する場合、embedded モードで利用したもの + α の jar が必要になる。

  1. Derby(derby.jar、derbynet.jar、derbyclient.jar) をクラスパスに追加

サンプルコード

package jp.in_vitro.codelets.derby;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.derby.drda.NetworkServerControl;

public class Codelet3 {

    public Codelet3() {
        super();
    }

    public static void main(final String[] args) throws Exception {
        Codelet3 me = new Codelet3();
        me.execute();
    }

    protected void execute() throws Exception {

        // Derby の起動。
        DerbyServer server = new DerbyServer();
        server.start();
        while (!server.isInitialized()) {
            Thread.sleep(1000);
        }

        // ネットワーク(と言っても loopback)経由で Derby に接続。
        DerbyClient client = new DerbyClient();
        client.initializeClient();
        client.execute();

        // Derby の終了。
        server.stop();
    }

    public static class DerbyClient {
        private static final String DRIVER = "org.apache.derby.jdbc.ClientDriver";
        private static final String PROTOCOL = "jdbc:derby://localhost:1527/";

        public DerbyClient() throws ClassNotFoundException {
            super();
            this.initializeClient();
        }

        public void execute() throws SQLException {

            Connection connection = null;
            try {
                connection = this.getConnection();
                Statement statement = connection.createStatement();
                ResultSet rs = statement
                        .executeQuery("SELECT * FROM sys.systables");
                while (rs.next()) {
                    System.out.println("" + rs.getString(1));
                }

                rs.close();
                connection.commit();
                connection.close();

            } finally {
                if (connection != null) {
                    connection.close();
                }
            }
        }

        protected void initializeClient() throws ClassNotFoundException {
            Class.forName(DRIVER);
        }

        protected Connection getConnection() throws SQLException {

            Connection connection = null;
            Properties props = new Properties();
            props.put("user", "me");
            props.put("password", "password");

            connection = DriverManager.getConnection(PROTOCOL
                    + "derbyDB;create=true", props);
            connection.setAutoCommit(false);

            return connection;
        }
    }

    public static class DerbyServer implements Runnable {
        private static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
        private static final String PROTOCOL = "jdbc:derby:";
        private Thread thread;
        private boolean initialized;

        public DerbyServer() {
            super();
        }

        public void start() {
            if (this.thread == null) {
                this.thread = new Thread(this);
                this.thread.start();
            }
        }

        public void stop() {
            if (this.thread != null) {
                this.thread = null;
            }
        }

        public void run() {
            try {
                this.initializeDBMS();
                while (this.thread != null) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    }
                }
                this.destroyDBMS();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public boolean isInitialized() {
            return this.initialized;
        }

        protected void initializeDBMS() throws Exception {

            System.setProperty("derby.drda.startNetworkServer", "true");
            Class.forName(DRIVER).newInstance();

            NetworkServerControl server = new NetworkServerControl();
            server.ping();

            this.initialized = true;
        }

        protected void destroyDBMS() throws SQLException {

            this.initialized = false;

            Connection connection = null;
            try {
                connection = DriverManager.getConnection(PROTOCOL
                        + ";shutdown=true");
                connection.commit();

            } catch (SQLException e) {
                // Derby が正常終了すると SQLException が発行される。
                System.out.println("" + e.getLocalizedMessage() + " : "
                        + e.getErrorCode());
            } finally {
                if (connection != null) {
                    connection.close();
                }
            }
        }
    }
}

Posted in Derby | このエントリーをはてなブックマークに追加 | この記事をクリップ! livedoor クリップ |