You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by yangjiajun <13...@qq.com> on 2020/05/06 13:02:03 UTC

Create table if not exists does not works in cocurrent case

Hello.I test creating same table in two threads.Here is my test code:

public class ConcurrentCreateTable {
    private static Connection conn;

    private static Connection conn1;

    public static void main(String[] args) throws Exception {

        initialize();
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try (Statement stmt = conn.createStatement()) {
                        stmt.execute(
                            "CREATE TABLE IF NOT EXISTS city1(ID
INTEGER,NAME VARCHAR ,PRIMARY KEY(ID)) WITH \"template=replicated\";");
                        stmt.execute("DROP TABLE IF EXISTS city1");
                    } catch (SQLException e) {

                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try (Statement stmt = conn1.createStatement()) {
                        stmt.execute(
                            "CREATE TABLE IF NOT EXISTS city1(ID
INTEGER,NAME VARCHAR ,PRIMARY KEY(ID)) WITH \"template=replicated\";");
                        stmt.execute("DROP TABLE IF EXISTS city1");
                    } catch (SQLException e) {

                        e.printStackTrace();
                    }
                }
            }
        }).start();
        while(true) {
            
        }
    }

    private static void initialize() throws Exception {
        Class.forName(Config.IGNITE_DRIVER);
        final Properties props = new Properties();
        conn = DriverManager.getConnection(Config.IGNITE_URL, props);
        conn1 = DriverManager.getConnection(Config.IGNITE_URL, props);
    }
}

It throws table already exists error.Does ignite not allow to cocurrently
create tables?



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/

Re: Create table if not exists does not works in cocurrent case

Posted by akorensh <al...@gmail.com>.
Hi,
 You are correct.  If the requests are simultaneous, the error will occur. A
very small delay(see below),  will prevent this.
  This exception is informational in nature, and could safely be ignored.

Try this for the second thread
        while (true) {
*                  Thread.sleep(100);*
                    try (Statement stmt = conn1.createStatement()) {
                        stmt.execute(
                            "CREATE TABLE IF NOT EXISTS city1(ID
INTEGER,NAME VARCHAR ,PRIMARY KEY(ID)) WITH \"template=replicated\";");
                        stmt.execute("DROP TABLE IF EXISTS city1");
                    } catch (SQLException e | InterruptedException e) {

                        e.printStackTrace();
                    }

Thanks, Alex



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/