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.
- @DataSourceDefinition(name = "java:app/env/Servlet_DataSource",
- minPoolSize = 0,
- initialPoolSize = 0,
- className = "org.apache.derby.jdbc.ClientXADataSource",
- user = "APP",
- password = "APP",
- databaseName = "testdb",
- 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.
- @DataSourceDefinitions(
- value = {
- @DataSourceDefinition(name = "java:app/env/HelloStatefulEJB_DataSource",
- minPoolSize = 0,
- initialPoolSize = 0,
- className = "org.apache.derby.jdbc.ClientXADataSource",
- portNumber = 1527,
- serverName = "localhost",
- user = "APP",
- password = "APP",
- databaseName = "testdb",
- properties = {"connectionAttributes=;create=true"}
- ),
- @DataSourceDefinition(name = "java:comp/env/HelloStatefulEJB_DataSource",
- minPoolSize = 0,
- initialPoolSize = 0,
- className = "org.apache.derby.jdbc.ClientXADataSource",
- portNumber = 1527,
- serverName = "localhost",
- user = "APP",
- password = "APP",
- databaseName = "testdb",
- properties = {"connectionAttributes=;create=true"}
- )
- }
- )
- @Stateful
- public class HelloStatefulEJB implements HelloStateful {
- ...
- ...
- }
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.
- <data-source>
- <name>java:app/env/Application_Level_DataSource</name>
- <class-name>org.apache.derby.jdbc.ClientXADataSource</class-name>
- <server-name>localhost</server-name>
- <port-number>1527</port-number>
- <database-name>testdb</database-name>
- <user>APP</user>
- <password>APP</password>
- <property>
- <name>connectionAttributes</name>
- <value>;create=true</value>
- </property>
- </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.