3.6.2.4. Enhancing Dynamically at Runtime
If a javaagent is not provided via the command line, OpenJPA will attempt to dynamically load the Enhancer that was mentioned in the previous section.
OpenJPA uses the Attach API of the jdk.attach module to load its agent into the running JVM.
This requires that:
-
the OpenJPA classes are loaded by the system class loader,
-
the
jdk.attachmodule is resolved at startup. This is the case for an application on the class path of a full JDK. An application that runs as a named module (java -m) needs--add-modules jdk.attach, and a runtime image created withjlinkmust contain the module, and -
the JVM is started with
-Djdk.attach.allowAttachSelf=true. Without this system property the JVM does not allow a process to attach to itself.
On Java 21 and later the JVM prints a warning when an agent is loaded dynamically (JEP 451). Start the JVM with -XX:+EnableDynamicAgentLoading to suppress it.
If one of the requirements is not met, OpenJPA logs an informational message once that names the missing setting and continues without the dynamic enhancer.
Applications that enhance their classes at build time or with the javaagent do not need the dynamic enhancer and can set openjpa.DynamicEnhancementAgent to false to avoid this message.
This support is provided as an ease of use feature and it is not recommended for use in a production system. Using this method of enhancement has the following caveats:
-
The dynamic runtime enhancer is plugged into the JVM during creation of the EntityManagerFactory. Any Entity classes that are loaded before the EntityManagerFactory is created will not be enhanced.
-
The command line javaagent settings are not configurable when using this method of enhancement.
-
Just as with the Javaagent approach, if you declare a persistent class list, then OpenJPA will only search for metadata and try to enhance the listed classes.
When then dynamic enhancer is loaded, the following informational message is logged:
[java] jpa.enhancement INFO [main] openjpa.Runtime - OpenJPA dynamically loaded the class enhancer. Any classes that were not enhanced at build time will be enhanced when they are loaded by the JVM.
Setting the property openjpa.DynamicEnhancementAgent to false will disable this function.
java -Djdk.attach.allowAttachSelf=true -XX:+EnableDynamicAgentLoading com.xyz.Main