Some stumbling blocks
A few "remind-myself" notes about various stumbling blocks that I encountered while writing my first Java EE code for GlassFish:
- You need
$GLASSFISH_HOME/lib/javaee.jar
in the classpath when compiling. - The JMS Destination Resource must have the property
Name=PhysicalQueue
, orName=PhysicalTopic
for a topic. - Resouce injection by annotations doesn't work for arbitrary classes or JSP sources. It only works for container managed components like EJBs, servlets, servlet filters, JSP tag handlers, application-client "main" classes,
- The server log is spammed with a multitude of these messaging exceptions:
"com.sun.messaging.jmq.io.Packet cannot be cast to com.sun.messaging.jms.ra.DirectPacket"
Follow the instructions here to have JMS run in a separate process. Note: this problem has been fixed in the GlassFish v2.1 beta.
- The web-app XML schema for "web.xml" must be version 2.5 for annotations to work.
- For injection to work in application clients, the injection variables must be static. And in order to inject EJBs the client must be packaged in an EAR file and declared in "application.xml".
- Injection variables cannot be static in servlets and filters.
- Persistent entity classes must be placed in jars under "lib" in the EAR file.
- Servlet and filter objects should not have
EntityManager
s as instance variables, sinceEntityManager
s are not thread-safe. Instead, either use an EJB as an instance variable and let the EJB access theEntityManager
, or useEntityManagerFactory
to create a temporaryEntityManager
and manage the transactions yourself. - Any webapp that requires login needs a "sun-web.xml" that declares
<security-role-mapping>
, otherwise no one is allowed access. Or if the webapp is packaged in an EAR file, the same mapping can be declared in "sun-application.xml". Also, the<realm-name>
in "web.xml" cannot contain space characters. - If you change the admin password,
asadmin login
is needed to update the stored credentials in~/.asadminpass
. - Use
@TableGenerator
annotation for a portable way to generate primary keys for persistent@Entity
objects. The generated keys are filled in by the first call toEntityManager.persist()
, and the new key is committed byEntityManager.flush()
. - Log Toplink-generated SQL statements by adding the following property under
<persistence-unit>
in "persistence.xml":
<property name="toplink.logging.level" value="FINE"/>