0
\$\begingroup\$

For this project:

NetBeansProjects/EntAppWeb/
├── build.xml
├── EntAppWeb-ejb
│  ├── build.xml
│  ├── nbproject
│  │  ├── ant-deploy.xml
│  │  ├── build-impl.xml
│  │  ├── genfiles.properties
│  │  ├── private
│  │  │  ├── private.properties
│  │  │  └── private.xml
│  │  ├── project.properties
│  │  └── project.xml
│  └── src
│  └── conf
│  └── MANIFEST.MF
├── EntAppWeb-war
│  ├── build.xml
│  ├── nbproject
│  │  ├── ant-deploy.xml
│  │  ├── build-impl.xml
│  │  ├── genfiles.properties
│  │  ├── private
│  │  │  ├── private.properties
│  │  │  └── private.xml
│  │  ├── project.properties
│  │  └── project.xml
│  ├── setup
│  │  └── glassfish-resources.xml
│  ├── src
│  │  ├── conf
│  │  │  ├── MANIFEST.MF
│  │  │  └── persistence.xml
│  │  └── java
│  │  └── dur
│  │  ├── beans
│  │  │  ├── NextClient.java
│  │  │  └── NextClientLocal.java
│  │  └── jpa
│  │  ├── AbstractFacade.java
│  │  ├── ClientFacade.java
│  │  ├── ClientFacadeLocal.java
│  │  ├── Client.java
│  │  └── exceptions
│  │  ├── IllegalOrphanException.java
│  │  ├── NonexistentEntityException.java
│  │  ├── PreexistingEntityException.java
│  │  └── RollbackFailureException.java
│  └── web
│  ├── eagle.xhtml
│  ├── falcon.xhtml
│  ├── index.xhtml
│  ├── menu.xhtml
│  ├── next.xhtml
│  ├── parrot.xhtml
│  ├── template.xhtml
│  └── WEB-INF
│  └── web.xml
├── LICENSE
├── nbproject
│  ├── ant-deploy.xml
│  ├── build-impl.xml
│  ├── genfiles.properties
│  ├── private
│  │  ├── private.properties
│  │  └── private.xml
│  ├── project.properties
│  └── project.xml
├── README.md
└── src
 └── conf
 └── MANIFEST.MF

with this backing bean:

package dur.beans;
import dur.jpa.Client;
import dur.jpa.ClientFacadeLocal;
import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
@Named("nextClient")
@ApplicationScoped
public class NextClient implements NextClientLocal {
 @EJB
 private ClientFacadeLocal clientFacade;
 private int next = 1009;
 @Override
 public String getNext() {
 next++;
 Client client = clientFacade.find(next);
 return client.toString();
 }
}

and this facelets template client:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:f="http://java.sun.com/jsf/core"
 >
 <h:head></h:head>
 <h:body>
 This and everything before will be ignored
 <ui:composition template="template.xhtml">
 <ui:define name="navigation">
 <ui:include src="menu.xhtml"/>
 </ui:define>
 <ui:define name="main">
 <h1>bird</h1>
 <p>
 next #{nextClient.next}
 </p>
 </ui:define>
 </ui:composition>
 This and everything after will be ignored
 </h:body>
</html>

I want to move the dur.jpa package to its own project and use it as a library. Currently the database connection is done from within the Enterprise Application with:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
 <persistence-unit name="EntAppWeb-warPU" transaction-type="JTA">
 <jta-data-source>jdbc/legacy_resource</jta-data-source>
 <exclude-unlisted-classes>false</exclude-unlisted-classes>
 <properties>
 <property name="javax.persistence.schema-generation.database.action" value="create"/>
 </properties>
 </persistence-unit>
</persistence>

I still want to use the jdbc-resource on glassfish.

  1. Is it correct, within best practices, to use the jdbc-resource on glassfish?

  2. How would the Java SE library utilize the jdbc-resource on glassfish? How is the connection established?

See also:

asked Oct 31, 2014 at 21:21
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

There is no much code to review here, but I spotted that next is not thread-safe. You can use an AtomicInteger instead.

@EJB
private ClientFacadeLocal clientFacade;
private AtomicInteger next = new AtomicInteger(1009);
@Override
public String getNext() {
 Client client = clientFacade.find(next.incrementAndGet());
 return client.toString();
}
answered Oct 31, 2014 at 21:54
\$\endgroup\$
2
  • \$\begingroup\$ how do you know that it's not thread safe? Or, rather, how would I learn to recognize that? \$\endgroup\$ Commented Nov 1, 2014 at 22:02
  • \$\begingroup\$ @Thufir Read "Java Concurrency in Practice" \$\endgroup\$ Commented Oct 2, 2015 at 22:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.