pg.ddx.io  pljava-dev@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Subject: [Pljava-dev] Is it safe to use multi-threaded PL/Java in single-threaded postgres process?
Date: Sat, 15 Mar 2014 21:04:59 -0500
Message-ID: <94B1FE06-4C37-488D-84C3-E4E4B0C94D24@me.com> (raw)
In-Reply-To: <A832684586A84610AD7173AB42138F38@maumau>
References: <30A6B4B499494CED93770BBC4835840C@maumau>
	<A832684586A84610AD7173AB42138F38@maumau>

So, I really think this question has been definitively answered. If you can produce a test case of your concerns which demonstrates the issue, we can discuss it. But the simple answer to your question is simply: yes. Pl/java obeys the rules and works according to spec. It does the right thing in the right way using the best practices. 

> On Mar 15, 2014, at 8:56 PM, MauMau <maumau307 at gmail.com> wrote:
> 
> I got the reply in pgsql-hackers about this.  According to Tom's comment, I'm worried about PL/Java's design.
> 
> http://www.postgresql.org/message-id/11347.1394545919 at sss.pgh.pa.us
> 
> Is there any rationale that it's safe for PL/Java to:
> 
> * load multi-threaded Java VM in single-threaded postgres process (which is what Tom is concerned about)
> 
> * make multi-threaded Java VM load single-threaded pljava.so (which is what JNI specification says should not be done).
> 
> 
> ----- Original Message ----- From: "MauMau" <maumau307 at gmail.com>
> To: <pljava-dev at lists.pgfoundry.org>
> Sent: Saturday, March 08, 2014 7:21 PM
> Subject: Is it safe to use multi-threaded PL/Java in single-threaded postgres process?
> 
> 
>> Hello,
>> 
>> Is PL/Java safe to use in terms of its threading design?  I've just asked pgsql-hackers about this and am waiting for response, adding the sentence:
>> 
>> "To put the question in other words, is it safe to load a multi-threaded PL library in the single-threaded backend process, if the PL only calls SPI in the main thread?"
>> 
>> My understanding is:
>> 
>> * PL/Java (pljava.so) is linked with the JNI (Java Native Interface) library, libjvm.so, in JRE.  libjvm.so is linked with libpthread.so, because Java VM is multi-threaded.  SO, "ldd pljava.so" shows libjvm.so and libpthread.so. pljava.so doesn't seem to be built for multh-threading ---  none of -mt, -D_REENTRANT or -D_POSIX_C_SOURCE is specified when building it.
>> 
>> * When the application calls Java stored function, pljava.so calls a function in libjvm.so to create a JVM in the backend process, then invokes the user-defined Java method in the main thread.  The user-defined Java method calls JDBC methods to access database.  The JDBC method calls are translated to backend SPI function calls through JNI.
>> 
>> * The main thread can create Java threads using Java Thread API, and those threads can call JDBC methods.  However, PL/Java intercepts JDBC method calls and serializes SPI calls.  So, only one thread calls SPI functions at a time.  I'm wondering if this is the reason why PL/Java is safe for use.
>> 
>> 
>> What I'm concerned about is whether multi-threaded code (Java VM) can run safely in a single-threaded code (postgres).  I cannot point out what can be a particular problem with PL/Java, but in general, the mixture of single-threaded code and multi-threaded one seems to cause trouble around handling errno, memory and file handles/pointers.
>> 
>> FYI, JNI specification says that the code called from Java VM should be built for multi-threading as follows.  But postgres is not.
>> 
>> http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html#wp9502
>> 
>> [Excerpt]
>> Compiling, Loading and Linking Native Methods
>> Since the Java VM is multithreaded, native libraries should also be compiled and linked with multithread aware native compilers. For example, the -mt flag should be used for C++ code compiled with the Sun Studio compiler. For code complied with the GNU gcc compiler, the flags -D_REENTRANT or -D_POSIX_C_SOURCE should be used. For more information please refer to the native compiler documentation.
> 
> _______________________________________________
> Pljava-dev mailing list
> Pljava-dev at lists.pgfoundry.org
> http://lists.pgfoundry.org/mailman/listinfo/pljava-dev



view thread (13+ messages)  latest in thread

Message-ID: <94B1FE06-4C37-488D-84C3-E4E4B0C94D24@me.com>
Permalink:  ../94B1FE06-4C37-488D-84C3-E4E4B0C94D24@me.com/
Also on:    postgresql.org/message-id/94B1FE06-4C37-488D-84C3-E4E4B0C94D24@me.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pljava-dev@postgresql.org
  Subject: Re: [Pljava-dev] Is it safe to use multi-threaded PL/Java in single-threaded postgres process?
  In-Reply-To: <94B1FE06-4C37-488D-84C3-E4E4B0C94D24@me.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox