X

Default DataSource in Java EE 7: Lesser XML and More Defaults (TOTD #196)



Section E.5.5.20 in the href="http://download.oracle.com/otndocs/jcp/java_ee-7-pr-spec/index.html">Java
EE 7 Platform specification defines a new Default Data Source
that must be provided by a Java EE 7-compliant runtime. This
pre-configured data source can be used by application for accessing
the associated database. It is accessible to the application under
the JNDI name:
java:comp/DefaultDataSource



This can be accessed in the application as:
@Resource(name="myDataSource", lookup="java:comp/DefaultDataSource")
DataSource myDS;



If the binding is not specified, the mapping of the reference will
default to this default data source. So the above fragment is
equivalent to:
@Resource(name="myDataSource")
DataSource myDS;



Section 8.2.1.5 in the href="http://download.oracle.com/otndocs/jcp/persistence-2_1-pr-spec/index.html">JPA
2.1 specification says



If neither jta-data-source and non-jta-data-source
elements are specified, the deployer must specify a JTA data
source at deployment or a JTA data source must be provided by
the container, and a JTA
EntityManagerFactory will be created
to correspond to it.


Per.book


This means a Java EE 7 application can have the following
persistence.xml:


<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="myPU" transaction-type="JTA">
<jta-data-source>java:comp/DefaultDataSource</jta-data-source>
</persistence-unit>
</persistence>


This is semantically equivalent to:


<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="movieplex7PU" transaction-type="JTA"/>
</persistence>


Notice, no jta-data-source.



In both cases, the default data source will be provisioned and
available to the application. In GlassFish 4, this is bound to the
JDBC resource jdbc/__default.



And this can be verified by giving the command:
./bin/asadmin list-jdbc-resources
jdbc/sample
jdbc/__TimerPool
jdbc/__default
Command list-jdbc-resources executed successfully.


Lesser XML and more defaults makes Java EE 7 more lightweight and
user friendly!



Download href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/">GlassFish
4.0 promoted build and try it today!


Join the discussion

Comments ( 3 )
  • guest Wednesday, January 23, 2013

    Is this:

    @Resource(name="myDataSource")

    DataSource myDS;

    equivalent to:

    @Resource(name="doesNotMatter")

    DataSource pickAnyNameDS;

    In other words what's the point of giving the data-source a name since I'm going to be using it via the variable after the resource-annotation anyway?


  • Arun Gupta Tuesday, January 29, 2013

    You just found a bug in the Java EE 7 platform spec. I took the excerpt from EE.5.20 section of the spec as is. After your comment, I discussed this with Java EE spec leads and this will now get clarified.

    Thanks,

    Arun


  • Vimal Thursday, March 7, 2013

    Hi Arun,

    Can I make java:comp/DefaultDatasource bind to a different jdbc source e.g jdbc/sample?

    Thx

    Vimal


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha
Oracle

Integrated Cloud Applications & Platform Services