Hi,

I've just spent half a day figuring out how to enable Tomcat's database
connection pooling for DSpace and wanted to share some notes here. The best
general information about JNDI configuration is is the Tomcat
documentation[0], but the DSpace configuration reference for version 6.x[1]
also shares a few pieces of information as well. I'm using DSpace 5.5 with
Tomcat 7 on both macOS and Ubuntu 16.04, so keep that in mind if you're
following along. This is probably better written as a blog post or wiki
page, but I'm recording it here for now.

First, I added a JDBC Resource to the GlobalNamingResources in Tomcat's
server.xml:

<Resource name="jdbc/dspace" auth="Container" type="javax.sql.DataSource"
      driverClassName="org.postgresql.Driver"
      url="jdbc:postgresql://localhost:5432/dspace"
      username="dspace"
      password="dspace"
      initialSize='5'
      maxActive='50'
      maxIdle='15'
      minIdle='5'
      maxWait='5000'
      validationQuery='SELECT 1'
      testOnBorrow='true' />

The pool size and idle settings come from a comment by Mark Wood on the
DS-3564 ticket on DSpace's issue tracker[2]. I will monitor the database
connections and DSpace logs and adjust as needed.

Second, I added a ResourceLink to each web application's context XML file,
ie for solr.xml:

<Context docBase="/Users/aorth/dspace/webapps/solr">
  <ResourceLink global="jdbc/dspace" name="jdbc/dspace"
type="javax.sql.DataSource"/>
</Context>

Third, I had to manually download a JDBC driver (latest for JDK 8 is
postgresql-42.1.4.jar) from PostgreSQL's website[3] and copy it to Tomcat's
library path. This was a bit of a mystery to me, because each DSpace web
application bundles a copy of the PostgreSQL JDBC driver for its own
connections, but Tomcat doesn't use any of those when configured to use a
global JDBC resource (could be that they aren't available when Tomcat
starts?). Manually copying one of the web application's
postgresql-9.1-901-1.jdbc4.jar files to Tomcat's library path does work,
but I felt it was cleaner to use the upstream driver from PostgreSQL's
website than to depend on a file from the DSpace build. Also, on my Ubuntu
16.04 system I tried installing the libpostgresql-jdbc-java package but it
didn't work. In any case, just know that you'll need to get a JDBC driver
and copy it to Tomcat's library path.

Last, I uncommented the db.jndi line in dspace.cfg. I left all the old
settings like db.username, db.password, etc intact because DSpace falls
back to those if the JDBC resource is not available and it seems to expect
them to be defined even if they aren't used.

After restarting Tomcat the effect of using a correctly configured global
database connection pool is visible instantly. For months and years I've
been struggling with database connection issues due to load from bots,
users, harvesters, etc. In the attached munin monitoring graph you can see
that the number of idle database connections dropped drastically after
deploying this. With the default DSpace settings every web application gets
its own pool of the size specified in dspace.cfg—that's right, the default
connection pool in dspace.cfg is not shared between web applications as the
comment would lead you to believe! This becomes very hard to manage on a
busy site where you need to keep increasing the maximum number of
connections in both dspace.cfg and the system's PostgreSQL configuration.
Also, there is no way to tell how many connections each of your web
applications is using. As you can see in the graph, there were a large
amount of idle connections, perhaps ten or more per web application
depending on your db.maxidle setting. Looking in pg_stat_activity I can see
some of those connections had been around for days or weeks even. Now all
DSpace web applications share one connection pool and the number of active
connections rises and falls according to need. It will be interesting to
see how the graphs look in a few weeks of running this on our development
server.

Now a few notes that should be addressed on the bug tracker perhaps:

- There is no indication in the DSpace logs that your JNDI resource is
being used, although you do see many errors in the logs when the
configuration is NOT successful, ie "Error retrieving JNDI context:
jdbc/dspace". You only know it is working if you see "PostgreSQL JDBC
Driver" in the application_name column of the PostgreSQL's pg_stat_activity
table.
- The dspace command line tools don't seem to be able to use the JNDI
resource and fall back to connecting to PostgreSQL directly. When
monitoring the pg_stat_activity table while running `dspace database info`
I noticed that eight (8) database connections were made by this one tool
alone!

I hope this proves to be useful. I'm happy to hear people's thoughts or
comments on this!

Cheers,

[0]
https://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html#PostgreSQL
[1] https://wiki.duraspace.org/display/DSDOC6x/Configuration+Reference
[2] https://jira.duraspace.org/browse/DS-3564
[3] https://jdbc.postgresql.org/download.html
-- 

Alan Orth
[email protected]
https://picturingjordan.com
https://englishbulgaria.net
https://mjanja.ch

-- 
You received this message because you are subscribed to the Google Groups 
"DSpace Technical Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/dspace-tech.
For more options, visit https://groups.google.com/d/optout.

Reply via email to