tomcat-jndi.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. .. _tomcat_jndi:
  2. Setting up a JNDI connection pool with Tomcat
  3. =============================================
  4. This tutorial walks the reader through the procedures necessary to setup a Oracle JNDI connection pool in Tomcat 6 and how to retrieve it from GeoServer. In the last section other two examples of configuration are described
  5. with PostGIS and SQLServer.
  6. Tomcat setup
  7. ------------
  8. In order to setup a connection pool Tomcat needs a JDBC driver and the necessary pool configurations.
  9. First off, you need to find the JDBC driver for your database. Most often it is distributed on the website of your DBMS provider, or available in the installed version of your database.
  10. For example, a Oracle XE install on a Linux system provides the driver at :file:`/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/jdbc/lib/ojdbc14.jar`, and that file needs to be moved into Tomcat shared libs directory, :file:`{TOMCAT_HOME}/lib`
  11. .. note:: be careful to remove the jdbc driver from the GeoServer WEB-INF/lib folder when copied to the Tomcat shared libs, to avoid issues in JNDI DataStores usage.
  12. Once that is done, the Tomcat configuration file :file:`{TOMCAT_HOME}/conf/context.xml` needs to be edited in order to setup the connection pool. In the case of a local Oracle XE the setup might look like:
  13. .. code-block:: xml
  14. <Context>
  15. ...
  16. <Resource name="jdbc/oralocal"
  17. auth="Container"
  18. type="javax.sql.DataSource"
  19. driverClassName="oracle.jdbc.driver.OracleDriver"
  20. url="jdbc:oracle:thin:@localhost:1521:xe"
  21. username="xxxxx" password="xxxxxx"
  22. maxTotal="20"
  23. initialSize="0"
  24. minIdle="0"
  25. maxIdle="8"
  26. maxWait="10000"
  27. timeBetweenEvictionRunsMillis="30000"
  28. minEvictableIdleTimeMillis="60000"
  29. testWhileIdle="true"
  30. poolPreparedStatements="true"
  31. maxOpenPreparedStatements="100"
  32. validationQuery="SELECT SYSDATE FROM DUAL"
  33. maxAge="600000"
  34. rollbackOnReturn="true"
  35. />
  36. </Context>
  37. .. note:: The above configuration is valid for Tomcat 8+, while Tomcat 7.x would use ``maxActive`` in place of ``maxTotal``.
  38. The example sets up a connection pool connecting to the local Oracle XE instance.
  39. The pool configuration shows is quite full-fledged:
  40. * at most 20 active connections (max number of connection that will ever be used in parallel)
  41. * at most 3 connections kept in the pool unused
  42. * prepared statement pooling (very important for good performance)
  43. * at most 100 prepared statements in the pool
  44. * a validation query that double checks the connection is still alive before actually using it (this is not necessary if there is guarantee the connections will never drop, either due to the server forcefully closing them, or to network/maintenance issues).
  45. .. warning:: Modify following settings only if you really know what you are doing. Using too low values for ``removedAbandonedTimeout`` and ``minEvictableIdleTimeMillis`` may result in connection failures, if so try to setup ``logAbandoned`` to ``true`` and check your ``catalina.out`` log file.
  46. Other parameters to setup connection pool:
  47. * timeBetweenEvictionRunsMillis (default -1) The number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run.
  48. * numTestsPerEvictionRun (default 3) The number of objects to examine during each run of the idle object evictor thread (if any).
  49. * minEvictableIdleTimeMillis (default 1000 * 60 * 30) The minimum amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any).
  50. * removeAbandoned (default false) Flag to remove abandoned connections if they exceed the removeAbandonedTimout. If set to true a connection is considered abandoned and eligible for removal if it has been idle longer than the removeAbandonedTimeout. Setting this to true can recover db connections from poorly written applications which fail to close a connection.
  51. * removeAbandonedTimeout (default 300) Timeout in seconds before an abandoned connection can be removed.
  52. * logAbandoned (default false) Flag to log stack traces for application code which abandoned a Statement or Connection.
  53. For more information about the possible parameters and their values refer to the `DBCP documentation <http://commons.apache.org/dbcp/configuration.html>`_.
  54. GeoServer setup
  55. ---------------
  56. Login into the GeoServer web administration interface and configure the datastore.
  57. First, choose the *Oracle (JNDI)* datastore and give it a name:
  58. .. figure:: oracle_start.png
  59. :align: center
  60. *Choosing a JNDI enabled datastore*
  61. Then, configure the connection parameters so that the JNDI path matches the one specified in the Tomcat configuration:
  62. .. figure:: oracle_conf.png
  63. :align: center
  64. *Configuring the JNDI connection*
  65. When you are doing this, make sure the *schema* is properly setup, or the datastore will list all the tables it can find in the schema it can access. In the case of Oracle the schema is usually the user name, upper cased.
  66. Once the datastore is accepted the GeoServer usage proceeds as normal.
  67. Other examples
  68. --------------
  69. Configuring a PostgreSQL connection pool
  70. ++++++++++++++++++++++++++++++++++++++++
  71. In this example a PostgreSQL connection pool will be configured.
  72. For configuring the JNDI pool you need to move the Postgres JDBC driver (it should be named :file:`postgresql-XX.X.X.jar`) from the GeoServer
  73. :file:`WEB-INF/lib` folder and put it into the :file:`{TOMCAT_HOME}/lib` folder.
  74. Then the following code must be added to the Tomcat configuration file :file:`{TOMCAT_HOME}/conf/context.xml` inside a Context tag.
  75. .. code-block:: xml
  76. <Context>
  77. <Resource name="jdbc/postgres"
  78. auth="Container"
  79. type="javax.sql.DataSource"
  80. driverClassName="org.postgresql.Driver"
  81. url="jdbc:postgresql://localhost:5432/test"
  82. username="xxxxx" password="xxxxxx"
  83. maxTotal="20"
  84. initialSize="0"
  85. minIdle="0"
  86. maxIdle="8"
  87. maxWait="10000"
  88. timeBetweenEvictionRunsMillis="30000"
  89. minEvictableIdleTimeMillis="60000"
  90. testWhileIdle="true"
  91. validationQuery="SELECT 1"
  92. maxAge="600000"
  93. rollbackOnReturn="true"
  94. />
  95. </Context>
  96. GeoServer setup
  97. ```````````````
  98. Login into the GeoServer web administration interface.
  99. First, choose the *PostGIS (JNDI)* datastore and give it a name:
  100. .. figure:: postgis_start.png
  101. :align: center
  102. Then configure the associated parameters. The value for jndiReferenceName corresponds to the Resource name given in :file:`{TOMCAT_HOME}/conf/context.xml`.
  103. .. figure:: postgis_conf.png
  104. :align: center
  105. Configuring a SQLServer connection pool
  106. +++++++++++++++++++++++++++++++++++++++
  107. For configuring the connection pool for SQLServer you need to configure the SQLServer drivers as explained in the :ref:`Microsoft SQL Server <data_sqlserver>` section
  108. and put the jar file into the :file:`{TOMCAT_HOME}/lib` folder.
  109. Then the following code must be written in the Tomcat configuration file :file:`{TOMCAT_HOME}/conf/context.xml`
  110. .. code-block:: xml
  111. <Context>
  112. ...
  113. <Resource name="jdbc/sqlserver"
  114. auth="Container"
  115. type="javax.sql.DataSource"
  116. driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
  117. url="jdbc:sqlserver://localhost:1433;databaseName=test;user=admin;password=admin;"
  118. username="admin" password="admin"
  119. maxTotal="20"
  120. initialSize="0"
  121. minIdle="0"
  122. maxIdle="8"
  123. maxWait="10000"
  124. timeBetweenEvictionRunsMillis="30000"
  125. minEvictableIdleTimeMillis="60000"
  126. testWhileIdle="true"
  127. poolPreparedStatements="true"
  128. maxOpenPreparedStatements="100"
  129. validationQuery="SELECT 1"
  130. maxAge="600000"
  131. rollbackOnReturn="true"
  132. />
  133. </Context>
  134. .. note:: Note that database name, username and password must be defined directly in the URL.
  135. GeoServer setup
  136. ```````````````
  137. Login into the GeoServer web administration interface.
  138. First, choose the *Microsoft SQL Server (JNDI)* datastore and give it a name:
  139. .. figure:: sqlserver_start.png
  140. :align: center
  141. Then configure the associated params:
  142. .. figure:: sqlserver_conf.png
  143. :align: center