# Help with crash in Oracle with threads

**URL:** <https://discourse.julialang.org/t/help-with-crash-in-oracle-with-threads/56464>\
**Category:** General Usage\
**Created:** [March 4, 2021, 8:18am UTC](https://discourse.julialang.org/t/help-with-crash-in-oracle-with-threads/56464 "2021-03-04T08:18:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [March 4, 2021, 8:18am UTC](https://discourse.julialang.org/t/help-with-crash-in-oracle-with-threads/56464/1 "2021-03-04T08:18:52Z")

</div>

Hello, I need some help with this crash.

I’m using Julia 1.5.3

It appears that when using threading, this crashes Julia. The crash disappears when using a simple for loop.

Here is a simple MWE

```julia
using Oracle
conn = Oracle.Connection(user, password, connstring)

Oracle.execute(conn, "CREATE TABLE TESTING_CRASH (TESTID NUMBER)")

```

This for loop works fine

```julia
for i = 1:100
    Oracle.execute(conn, "INSERT INTO testing_crash (TESTID) VALUES ($i)")
end

```

This threaded for loop causes Julia to crash

```julia
Threads.@threads for i = 1:100
    Oracle.execute(conn, "INSERT INTO testing_crash (TESTID) VALUES ($i)")
end

```

Anyone can help me with this?

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [March 4, 2021, 2:57pm UTC](https://discourse.julialang.org/t/help-with-crash-in-oracle-with-threads/56464/2 "2021-03-04T14:57:22Z")

</div>

I do not believe accessing a connection is thread safe (for any database). Each thread should have it’s own connection. Connection Pooling [Tutorial · Oracle.jl](https://felipenoris.github.io/Oracle.jl/stable/tutorial/#Session-Pools) is normally the way you handle this. You create a pool size of say 20 connections and each thread takes a connection when needed and returns it when done. If there are no available connections then the thread waits for one to be returned.

The key is to have each thread use the connection for the minimum amount of time, so query for the information, return the connection, THEN process the data.

The size of the connection pool really depends on your queries and database server (so there is no single magic number for all). Too many and you are wasting server resources or worse causing your db server to thrash. Too few and you are not fully utilizing your database.

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [March 4, 2021, 3:59pm UTC](https://discourse.julialang.org/t/help-with-crash-in-oracle-with-threads/56464/3 "2021-03-04T15:59:34Z")

</div>

OK, thanks, that kind of makes sense. I guess I didn’t see any issues when running LibPQ like this so I thought Oracle should run the same way.

But in any case, it shouldn’t crash Julia, should it?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [March 4, 2021, 4:13pm UTC](https://discourse.julialang.org/t/help-with-crash-in-oracle-with-threads/56464/4 "2021-03-04T16:13:13Z")

</div>

Oracle.jl calls C code. If the C code is not thread safe, then corruption is likely to occur. This can overwrite pointers, then accessing corrupt pointers often results in a process crash.

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [March 4, 2021, 4:14pm UTC](https://discourse.julialang.org/t/help-with-crash-in-oracle-with-threads/56464/5 "2021-03-04T16:14:23Z")

</div>

The Oracle.jl code is really just a bridge into the C library so I suspect it’s the C library that is causing memory corruption or something. So i don’t think there is much that can be done in Julia. The author of Oracle.jl could provide locking around the execute calls however that would be an overhead they might not want to incur.

---

<div class="post-metadata">

**Author:** ![kubo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kubo/32/4709_2.png) [@kubo](https://discourse.julialang.org/u/kubo)\
**Post date:** [March 5, 2021, 3:26am UTC](https://discourse.julialang.org/t/help-with-crash-in-oracle-with-threads/56464/6 "2021-03-05T03:26:15Z")

</div>

Oracle.jl uses OPDI-C, which isn’t thread safe when [`DPI_MODE_CREATE_THREADED`](https://oracle.github.io/odpi/doc/enums/dpiCreateMode.html) isn’t set. Oracle.jl seems not to set it.
