Define a data source in Java EE 6

Source: Internet
Author: User

In Java EE applications, the data source object DataSource objects is a way to access relational databases through the jdbc api. Each DataSource object has a series of attributes used to describe the data source in the real world. These attributes can be used to describe the address, database name, network protocol, and other information of the database server. In addition, the data source object can be used with JNDI. If you bind a data source to a registered JNDI service, you can access the jndi api to obtain the data source object in the application, then, you can obtain the link to the database and perform database operations.

Before Java EE 6, the definition and use of data sources were strongly correlated with Java application server manufacturers. JavaEE 6 provided a more flexible data source definition method, data sources compatible with multiple application servers can be defined. In addition, multiple definitions, such as comments, declarations, and deployment expressions, are supported. This article describes how to use annotations and deployment descriptions to define data sources.

Use annotations to define data sources

Java EE 6 provides two annotation tags used to define the data source: @ cecedefinition and @ DataSourceDefinitions, both of which are located in the javax. annotation. SQL package. You can use @ cecedefinition to define a single data source, and use @ DataSourceDefinitions to define multiple data sources.

@ Cecedefinition Annotation

@ Cecedefinition annotation mark can be used to define data sources in components such as the application client, Servlet, and EJB. With this annotation mark, you can define data source objects and bind them to JNDK. The usage of this tag is no different from setting the attributes of the data source object. You can also set additional information and specific information.

For example, the following annotation marks the creation of a data source accessing the Derby database.

 
 
  1. @DataSourceDefinition(name = "java:app/env/Servlet_DataSource",  
  2.     minPoolSize = 0,  
  3.     initialPoolSize = 0,  
  4.     className = "org.apache.derby.jdbc.ClientXADataSource",  
  5.     user = "APP",  
  6.     password = "APP",  
  7.     databaseName = "testdb",  
  8.     properties = {"connectionAttributes=;create=true"}  

The name property value of the data source is the JNDI name to be bound. According to the characteristics of JNDI, this value should be unique. From the above example, we can note that the first part of the attribute value, that is, java: app is a namespace description. The Application Component namespace is introduced in JavaEE 6. Currently, the following namespaces can be used:

Java: comp, which can be accessed by components in the same application and container.

Java: module, which can be accessed in a module with the same element, such as an EJB element defined in the same ejb-jar.xml.

Java: app, which can be accessed in all components and modules of the same application, such as client components, WEB components, and EJB components in the unified ear file.

Java: global, which can be shared among all applications on the server.

The namespace range referenced by the name attribute also determines the access scope of the data source object, that is, all applications in the module, application, or the same service. The following definitions are described as follows:

Java: comp/env/. Other components in the same container as the data source definition component, such as client components, WEB components, and EJB components, can access this data source.

Java: module/env/, which can be accessed by components located in the same ejb-jar.xml.

Java: app/env/. All components in the same application, such as EJB, servlet, and client components, can access the data source.

@ Cecedefinitions Annotation

@ Cecedefinitions can be used to create multiple data sources in a component class. Just like defining an array, each data source is an element of an array. The following is an example of defining multiple data sources for EJB.
 

 
 
  1. @DataSourceDefinitions(  
  2.     value = {  
  3.         @DataSourceDefinition(name = "java:app/env/HelloStatefulEJB_DataSource",  
  4.             minPoolSize = 0,  
  5.             initialPoolSize = 0,  
  6.             className = "org.apache.derby.jdbc.ClientXADataSource",  
  7.             portNumber = 1527,  
  8.             serverName = "localhost",  
  9.             user = "APP",  
  10.             password = "APP",  
  11.             databaseName = "testdb",  
  12.             properties = {"connectionAttributes=;create=true"}  
  13.         ),  
  14.         @DataSourceDefinition(name = "java:comp/env/HelloStatefulEJB_DataSource",  
  15.             minPoolSize = 0,  
  16.             initialPoolSize = 0,  
  17.             className = "org.apache.derby.jdbc.ClientXADataSource",  
  18.             portNumber = 1527,  
  19.             serverName = "localhost",  
  20.             user = "APP",  
  21.             password = "APP",  
  22.             databaseName = "testdb",  
  23.             properties = {"connectionAttributes=;create=true"}  
  24.         )  
  25.     }  
  26. )  
  27. @Stateful 
  28. public class HelloStatefulEJB implements HelloStateful {  
  29.     ...  
  30.     ...  

Define data sources using deployment expressions

In addition to the previous approach, JavaEE 6 continues to support defining the data source using the method of deployment description, which can be written in application. xml, application-client.xml, web. xml and ejb-jar.xml. The data source defined in the following example has the same meaning as the previous @ cecedefinition data source.

 
 
  1. <data-source> 
  2.     <name>java:app/env/Application_Level_DataSource</name> 
  3.     <class-name>org.apache.derby.jdbc.ClientXADataSource</class-name> 
  4.     <server-name>localhost</server-name> 
  5.     <port-number>1527</port-number> 
  6.     <database-name>testdb</database-name> 
  7.     <user>APP</user> 
  8.     <password>APP</password> 
  9.     <property> 
  10.         <name>connectionAttributes</name> 
  11.         <value>;create=true</value> 
  12.     </property> 
  13. </data-source> 

If two data sources are defined through @ cecedefinition and deployment description in the same application, the two data sources have the same name attribute. In such a case, the priority of the data source defined by the deployment description is higher than that defined by the annotation statement.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.