I'm not on the list so please don't forget to CC me.
First, what I'm doing, briefly. I have multithreaded proxy-server (it is
based on few years ago abandoned project). It wants to store information on
every page load/refresh to the database. And this works in almost all
cases... Almost. It is obvious that this is not acceptable because I have
broken database as a result; it should work in all cases of course to be
useful in the real world.
Now, the details. Here is my functions to connect to the database and
initialize a statement:
private static Connection conn = null;
private static Statement stat = null;
final private void connect_to_psql()
if(mgr.loglevel>3) System.err.println("Connecting to PostgreSQL...");
// Step 1: Load the JDBC driver.
// Step 2: Establish the connection to the database.
String url = "jdbc:" + mgr.psql_url;
conn = DriverManager.getConnection(url, mgr.psql_user, mgr.psql_password);
catch (Exception e)
System.err.println("Got an exception when connecting to PostgreSQL database
final private void initialize_statement()
stat = conn.createStatement();
catch (SQLException e)
System.out.println("We got an exception while creating a PostgreSQL
statement: that probably means we're no longer connected. ");
if(conn == null)
if(stat == null)
conn and stat assigments - that's all is really important here. I call these
functions only once for all threads. Everything works great in almost
all cases but sometimes (see below what "sometimes" exactly means here) I get
this crash when I call stat.executeUpdate(query):
Exception in thread "Thread-518" java.lang.NullPointerException
This crash is "hard": it crashes whole thread (but not the main program of
course) even if I have related catch block. So I never have a chance to
reconnect to the database from the thread to "fix" the problem. It doesn't
matter at all what query I use. I even can use exactly the same query every
time (constant string) and still have JDBC to crashes *sometimes*.
Now, what "sometimes" means? Sometimes I get above error every 1-3 calls of
executeUpdate(), sometimes once every 200-400 calls (this is most common
case). In other words this is non-uniform, at least at first impression. From
the JDBC user point of view it doesn't matter what query I use or how big is
uptime of my proxy or database connection. This is just happens and I cannot
If I change "private static Statement stat = null;" to "private Statement
stat = null;" and get in this way indepedant stat variable for each thread
nothing changes. I still get java.lang.NullPointerException sometimes and it
is almost the same as one above (please note that crash above is with a
little older JDBC release and crash below is with newest JDBC release, as you
can see nothing changes - just minor difference in line numbers):
Exception in thread "Thread-108" java.lang.NullPointerException
Now, I see only two possibilities:
1) This is a bug in JDBC. Is JDBC thread-safe? Is it supposed to be
thread-safe by design? Obviously if not then initializing conn
every time will be inefficient and I need to write a separate server to
execute PSQL queries because I want to connect only once to the DB.
2) I'm doing multithreading with JDBC in a wrong way. Do I need anything
special? Can I use same conn in all threads?
Feel free to ask if you additional question. I think that above examples show
the whole problem because that's only JDBC code in my proxy currently.
Sorry if there is something obvious I have missed - this is a first time I
write multithreading code in Java.
pgsql-jdbc by date
|Next:||From: Kris Jurka||Date: 2007-08-05 02:57:50|
|Subject: Re: Strange crashes in JDBC code in multithreaded application.|
|Previous:||From: Laszlo Hornyak||Date: 2007-08-04 06:47:27|
|Subject: Re: statement caching patch from Laszlo Hornyak for review|