Search in sources :

Example 11 with SortedProperties

use of org.h2.util.SortedProperties in project h2database by h2database.

the class TestAutoServer method testAutoServer.

private void testAutoServer(boolean port) throws Exception {
    if (config.memory || config.networked) {
        return;
    }
    deleteDb(getTestName());
    String url = getURL(getTestName() + ";AUTO_SERVER=TRUE", true);
    if (port) {
        url += ";AUTO_SERVER_PORT=11111";
    }
    String user = getUser(), password = getPassword();
    Connection connServer = getConnection(url + ";OPEN_NEW=TRUE", user, password);
    int i = ITERATIONS;
    for (; i > 0; i--) {
        Thread.sleep(100);
        SortedProperties prop = SortedProperties.loadProperties(getBaseDir() + "/" + getTestName() + ".lock.db");
        String key = prop.getProperty("id");
        String server = prop.getProperty("server");
        if (server != null) {
            String u2 = url.substring(url.indexOf(';'));
            u2 = "jdbc:h2:tcp://" + server + "/" + key + u2;
            Connection conn = DriverManager.getConnection(u2, user, password);
            conn.close();
            int gotPort = Integer.parseInt(server.substring(server.lastIndexOf(':') + 1));
            if (port) {
                assertEquals(11111, gotPort);
            }
            break;
        }
    }
    if (i <= 0) {
        fail();
    }
    Connection conn = getConnection(url + ";OPEN_NEW=TRUE");
    Statement stat = conn.createStatement();
    if (config.big) {
        try {
            stat.execute("SHUTDOWN");
        } catch (SQLException e) {
            assertKnownException(e);
        // the connection is closed
        }
    }
    conn.close();
    connServer.close();
    deleteDb("autoServer");
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) SortedProperties(org.h2.util.SortedProperties)

Example 12 with SortedProperties

use of org.h2.util.SortedProperties in project h2database by h2database.

the class FileLock method lockSocket.

private void lockSocket() {
    method = SOCKET;
    properties = new SortedProperties();
    properties.setProperty("method", String.valueOf(method));
    setUniqueId();
    // if this returns 127.0.0.1,
    // the computer is probably not networked
    ipAddress = NetUtils.getLocalAddress();
    FileUtils.createDirectories(FileUtils.getParent(fileName));
    if (!FileUtils.createFile(fileName)) {
        waitUntilOld();
        long read = FileUtils.lastModified(fileName);
        Properties p2 = load();
        String m2 = p2.getProperty("method", SOCKET);
        if (m2.equals(FILE)) {
            lockFile();
            return;
        } else if (!m2.equals(SOCKET)) {
            throw getExceptionFatal("Unsupported lock method " + m2, null);
        }
        String ip = p2.getProperty("ipAddress", ipAddress);
        if (!ipAddress.equals(ip)) {
            throw getExceptionAlreadyInUse("Locked by another computer: " + ip);
        }
        String port = p2.getProperty("port", "0");
        int portId = Integer.parseInt(port);
        InetAddress address;
        try {
            address = InetAddress.getByName(ip);
        } catch (UnknownHostException e) {
            throw getExceptionFatal("Unknown host " + ip, e);
        }
        for (int i = 0; i < 3; i++) {
            try {
                Socket s = new Socket(address, portId);
                s.close();
                throw getExceptionAlreadyInUse("Locked by another process");
            } catch (BindException e) {
                throw getExceptionFatal("Bind Exception", null);
            } catch (ConnectException e) {
                trace.debug(e, "socket not connected to port " + port);
            } catch (IOException e) {
                throw getExceptionFatal("IOException", null);
            }
        }
        if (read != FileUtils.lastModified(fileName)) {
            throw getExceptionFatal("Concurrent update", null);
        }
        FileUtils.delete(fileName);
        if (!FileUtils.createFile(fileName)) {
            throw getExceptionFatal("Another process was faster", null);
        }
    }
    try {
        // 0 to use any free port
        serverSocket = NetUtils.createServerSocket(0, false);
        int port = serverSocket.getLocalPort();
        properties.setProperty("ipAddress", ipAddress);
        properties.setProperty("port", String.valueOf(port));
    } catch (Exception e) {
        trace.debug(e, "lock");
        serverSocket = null;
        lockFile();
        return;
    }
    save();
    locked = true;
    watchdog = new Thread(this, "H2 File Lock Watchdog (Socket) " + fileName);
    watchdog.setDaemon(true);
    watchdog.start();
}
Also used : UnknownHostException(java.net.UnknownHostException) BindException(java.net.BindException) IOException(java.io.IOException) SortedProperties(org.h2.util.SortedProperties) SortedProperties(org.h2.util.SortedProperties) Properties(java.util.Properties) IOException(java.io.IOException) DbException(org.h2.message.DbException) BindException(java.net.BindException) UnknownHostException(java.net.UnknownHostException) ConnectException(java.net.ConnectException) InetAddress(java.net.InetAddress) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ConnectException(java.net.ConnectException)

Example 13 with SortedProperties

use of org.h2.util.SortedProperties in project h2database by h2database.

the class FileLock method lockSerialized.

private void lockSerialized() {
    method = SERIALIZED;
    FileUtils.createDirectories(FileUtils.getParent(fileName));
    if (FileUtils.createFile(fileName)) {
        properties = new SortedProperties();
        properties.setProperty("method", String.valueOf(method));
        setUniqueId();
        save();
    } else {
        while (true) {
            try {
                properties = load();
            } catch (DbException e) {
            // ignore
            }
            return;
        }
    }
}
Also used : SortedProperties(org.h2.util.SortedProperties) DbException(org.h2.message.DbException)

Aggregations

SortedProperties (org.h2.util.SortedProperties)13 Properties (java.util.Properties)6 IOException (java.io.IOException)4 DbException (org.h2.message.DbException)4 File (java.io.File)3 SQLException (java.sql.SQLException)3 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 OutputStream (java.io.OutputStream)2 Connection (java.sql.Connection)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 SysProperties (org.h2.engine.SysProperties)2 InputStream (java.io.InputStream)1 LineNumberReader (java.io.LineNumberReader)1 RandomAccessFile (java.io.RandomAccessFile)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 BindException (java.net.BindException)1 ConnectException (java.net.ConnectException)1 InetAddress (java.net.InetAddress)1