Skip to content

Concurrent access to MonetDBe embedded database not possible. #123

@aris-koning

Description

@aris-koning

The basic problem is that MonetDBe-Python does not allow multiple concurrent connections. It uses class level state in the class Frontend (_active_context) to ensure that. The first thread that connects, gets a connection which is closed by the second thread that connects.

I think that this is a remnant from the time we assumed only one connection at a time in a MonetDBe process.

But this can now be relaxed. Take a look at this MonetDBe native example for how this can be done with pthreads.

When this is fixed, I expect something like the following Python code to work:


import monetdbe
import concurrent.futures

monet_conn_1 = monetdbe.connect("devdb", autocommit=True)
monet_conn_1.execute("CREATE TABLE foo (i INT);")

def write_table():
    monet_conn_2 = monetdbe.connect("devdb", autocommit=True)
    n = 0
    while n < 10:
        print("write_table")
        monet_conn_2.execute("INSERT INTO foo VALUES (10);")
        n=n+1

def read_table():
    monet_conn_3 = monetdbe.connect("devdb", autocommit=True)
    n = 0
    while n < 10:
        print("read_table")
        monet_conn_3.cursor().execute("SELECT * FROM foo;")
        print(len(monet_conn_3.cursor().fetchall()))
        n=n+1

with concurrent.futures.ThreadPoolExecutor() as executor:
    executor.submit(write_table)
    executor.submit(read_table)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions