is there a standard way to define a JDBC Datasource for Java EE containers?

Is there a standard way to define a JDBC datasource and deploy it?

Yes, there is. It’s done via the <data-source> element, which you can put in web.xml, ejb-jar.xml and application.xml. If you don’t like XML, you can also use an annotation for this instead: @DataSourceDefinition

Example of a web.xml entry

<data-source>
    <name>java:app/myDS</name>
    <class-name>org.postgresql.xa.PGXADataSource</class-name>
    <server-name>pg.myserver.com</server-name>
    <database-name>my_db</database-name>
    <user>foo</user>
    <password>bla</password>
    <transactional>true</transactional>
    <isolation-level>TRANSACTION_READ_COMMITTED</isolation-level>
    <initial-pool-size>2</initial-pool-size>
    <max-pool-size>10</max-pool-size>
    <min-pool-size>5</min-pool-size>
    <max-statements>0</max-statements>
</data-source>

Further reading:

p.s. I’m surprised all other answers say this doesn’t exist, while it clearly does, even at the time this question was originally asked.

Leave a Comment