Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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)

like image 988
radai Avatar asked Feb 17 '10 10:02

radai


People also ask

Is JDBC part of Java EE?

Java EE applications access relational databases through the JDBC API. A JDBC resource (data source) provides applications with a means of connecting to a database. Typically, a JDBC resource is created for each database accessed by the applications deployed in a domain.

How do you create a DataSource in Java?

To create and deploy a DataSource object, you need to perform these tasks: Create an instance of the appropriate DataSource implementation. Set the properties of the DataSource object. Register the object with the Java Naming and Directory Interface (JNDI) naming service.

What is a JDBC DataSource?

WebLogic JDBC data sources provide database access and database connection management. Each data source contains a pool of database connections that are created when the data source is created and at server startup.

What is difference between DataSource and DriverManager?

DataSource and the DriverManager are the two basic ways to connect to a database. The DriverManager is older facility, DataSource is newer. It is recommended to use the new DataSource facility to connect to databases and other resources. DataSource facility has several advantages over DriverManager facility.


1 Answers

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:

  • Introducing the DataSourceDefinition Annotation
  • The state of @DataSourceDefinition in Java EE
  • Example application use standard data source

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.

like image 65
Arjan Tijms Avatar answered Oct 12 '22 13:10

Arjan Tijms