Search code examples
javajdbcjakarta-eehsqldb

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


I know that for JBoss you need a [name]-ds.xml file in the /deploy subdirectory of the appropriate instance. i dont have any experience with other Java EE containers, but im trying to stick to standards as much as possible. is there a standard way to define a JDBC datasource and deploy it ? if possible i'd like to include my datasource inside the *.ear file (for instance, an embedded in-memory HSQLDB datasource for demo purposes) ?

if there is no standard way, will other containers at least accept the jboss way ? (/deploy/*-ds.xml)


Solution

  • 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.