[id='configuring-server-datasources_{context}']
= Configuring managed datasources

Create managed datasources as part of your {brandname} Server configuration to optimize connection pooling and performance for JDBC database connections.
You can then specify the JDNI name of the managed datasources in your caches, which centralizes JDBC connection configuration for your deployment.

.Prerequisites

* Copy database drivers to the `server/lib` directory in your {brandname} Server installation.
//Community content
ifdef::community[]
+
[TIP]
====
Use the [command]`install` command with the {brandname} Command Line Interface (CLI) to download the required drivers to the `server/lib` directory, for example:

[source,bash,options="nowrap",subs=attributes+]
----
install org.postgresql:postgresql:42.1.3
----
====
endif::community[]

.Procedure

. Open your {brandname} Server configuration for editing.
. Add a new `data-source` to the `data-sources` section.
. Uniquely identify the datasource with the `name` attribute or field.
. Specify a JNDI name for the datasource with the `jndi-name` attribute or field.
+
[TIP]
====
You use the JNDI name to specify the datasource in your JDBC cache store
configuration.
====
. Set `true` as the value of the `statistics` attribute or field to enable statistics for the datasource through the `/metrics` endpoint.
. Provide JDBC driver details that define how to connect to the datasource in the `connection-factory` section.
.. Specify the name of the database driver with the `driver` attribute or field.
.. Specify the JDBC connection url with the `url` attribute or field.
.. Specify credentials with the `username` and `password` attributes or fields.
.. Provide any other configuration as appropriate.
. Define how {brandname} Server nodes pool and reuse connections with connection pool tuning properties in the `connection-pool` section.
. Save the changes to your configuration.

.Verification

Use the {brandname} Command Line Interface (CLI) to test the datasource connection, as follows:

. Start a CLI session.
+
[source,options="nowrap",subs=attributes+]
----
$ bin/cli.sh
----
+
. List all datasources and confirm the one you created is available.
+
[source,options="nowrap",subs=attributes+]
----
[//containers/default]> server datasource ls
----
+
. Test a datasource connection.
+
[source,options="nowrap",subs=attributes+]
----
[//containers/default]> server datasource test my-datasource
----

[discrete]
== Managed datasource configuration

.XML
[source,xml,options="nowrap",subs=attributes+,role="primary"]
----
include::xml/server_datasources.xml[]
----

.JSON
[source,json,options="nowrap",subs=attributes+,role="secondary"]
----
include::json/server_datasources.json[]
----

.YAML
[source,yaml,options="nowrap",subs=attributes+,role="secondary"]
----
include::yaml/server_datasources.yaml[]
----
