About this task
You need to configure your JPA bundle and add the data sources
to WebSphere® Application
Server before you can access JPA persistence units from another bundle. For
example, a web application bundle that consumes JPA entities and displays
and manipulates data.
Tip: Ensure that your JPA persistence file
contains references to a Java™ Transaction API (JTA) and non-JTA data source.
JPA has two
transactional patterns for accessing a data source:
- jta-data-source
- The Java Transaction API
(JTA) resource pattern depends on global transactions. The JTA resource
pattern is typically used within the scope of an Enterprise JavaBeans (EJB) session facade. This
configuration allows the session bean to control transaction and security
contexts while JPA handles the persistence mappings. In this case,
the application does not use the EntityTransaction interface but relies
on the EntityManager enlisted with the global transaction when it
is accessed.
- non-jta-data-source
- The non-JTA resource pattern is used to deal with a single resource
in the absence of global transactions. The non-JTA resource pattern
is typically used within the scope of a web application or an application
client. The application controls the transaction with the data source
with the EntityTransaction interface.
In persistence.xml files
for an OSGi application, the jta-data-source and non-jta-data-source elements access the data sources through
a Java Naming and Directory
Interface (JNDI) lookup, a JNDI lookup to the service registry, or
through Blueprint.
If the JTA and non-JTA data sources are not
configured in the persistence.xml file, the default
JTA and non-JTA data sources configured for the server are used. By default
the values are null. Some JPA entity features require a non-JTA data
source to be specified. For example, automatic entity identity generation.
Procedure
- Ensure that the entity and entity controller packages are
added to manifest.mf as export packages:
- Double-click Manifest: <project_name>, where <project_name> is the name
of your JPA bundle project. The bundle manifest opens in the editor.
- Switch to the Runtime tab. Verify that the entities
and entity controller packages are added as export packages.
- Add non-JTA data sources to persistence.xml:
- Open persistence.xml in the editor.
- In the components list, select your entity to display
the details of your entity.
- In the Non-JTA data source field, type the global JNDI
name of a non-datasource. For example, osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/blogdbnojta).
- Modify the getEntityManager() method.
By modifying the getEntityManager() method, you are
configuring the JNDI lookup for the EntityManagerFactory service since the persistence unit in a JPA bundle is not in a Java EE environment.
You can
also configure the JNDI lookup by making the entity manager bean a
blueprint managed bean. For more information, see JPA and OSGi Applications.
- Open the entity manager bean in the editor.
- Locate getEntityManager() and modify
it as follows:
private EntityManager getEntityManager() {
try {
emf = (EntityManagerFactory) new InitialContext().lookup("osgi:service/javax.persistence.EntityManagerFactory/(osgi.unit.name=jpaBundle)");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return emf.createEntityManager();
}
Important: In the following line of code,
ensure that the persistence unit matches the persistence unit in
persistence.xml:
emf = (EntityManagerFactory) new InitialContext().lookup("osgi:service/javax.persistence.EntityManagerFactory/(osgi.unit.name=jpaBundle)");
- Fix generated errors on InitialContext and NamingException:
- Switch to the Markers view.
- For each error, right-click the error and select QuickFix. Follow the instructions in the wizard to import
the required packages.
- Add a JDBC provider to the WebSphere Application Server administrative
console:
- Switch to the Servers view.
- Right-click your server instance and select Start.
- Right-click your server instance and select to open the administrative console.
- Click .
- Click New in the JDBC providers
page. The Create a data source wizard opens.
- Follow the instructions in the wizard to create the
JDBC provider.
- Save your changes,
- Add data source definitions to the WebSphere Application Server admin console:
- In the administrative console, click to open the data sources page
in the console.
- In the Data sources page, click New to create a data source definition with a JNDI name set to the JTA
connection definition specified in persistence.xml. For example, jdbc/blogdb.
- In the Data sources page, click New to create another data source definition with a JNDI name set to
the non-JTA connection definition specified in persistence.xml. For example, jdbc/blogdbnojta.
- After the data source for the non-JTA connection is
created, click the definition for the non-JTA connection in the Data sources
page of the admin console. The Configuration page
opens.
- In the Additional Properties section, click WebSphere Application Server data source properties.
- Select Non-transactional data source. Within the application server, the use of the <non-jta-data-source> element requires a special configuration
for a non-transactional data source. Data sources that are configured
for the application server do not function as a <non-jta-data-source> because all data sources that are configured by the application
server are automatically enlisted with the current transactional context.
To prevent this automatic enlistment, add an additional data source
custom property nonTransactionalDataSource=true.
Results
You can now consume JPA entities, display, and manipulate
JPA data, where a JPA bundle is accessed from a web application bundle.