Use J2EE client and Hibernate under Eclipse

xiaoxiao2021-03-06  59

Eclipse loads each plug-in, each plugin uses a separate thread, so the same name object between the plug-in is different and cannot be converted. In this case, if some plugins need to load some class libraries otherwise, the class that is loaded with the Eclipse itself will not be converted. This problem highlights the performance on the J2EE client interface.

Why is the same name file loaded with different threads different in JVM? Please refer to the "deep-in-Java virtual machine" book, which is more detailed description.

In Eclipse, different plugins generate a thread by Eclipse, which is responsible for the loading of class files required by the corresponding plugin. This benefit is to avoid conflicts of class files that may exist between different plugins (such as issues caused by inconsistencies), but also brings different problems that are loaded between different plugins (for example, the two plugins are loaded. Log4j, the log4j between the two plugins is two different objects for JVM).

When using the J2EE structure, it is usually provided to the J2EE client various remote interfaces. Find the remote HOME interface through JNDI and enforce type conversion. E.g:

EXAMPLEHOME HOME = (ExampleHome) getContext (). Lookup (Example_jndi_name);

Note that by default, the object to look up through the lookup is loaded by sun.misc.launcher. However, ExampleHome referenced in the Eclipse plugin is loaded by Eclipse itself, so the conversion will result in Cast Error in the Eclipse environment. Solution is to let EXAMPLEHOME these remote interfaces are loaded by sun.misc.launcher, the easiest way is Put these packets into the Java system path, as system components are loaded by JVM.

Another solution is to create a specific InitialContextFactoryBuilder so that it is loaded from Eclipse.

NamingManager has a static method setInitialContextFactoryBuilder, you can install an InitialContextFactoryBuilder. With this, refer to Java implementation, you can write a specific CONTEXT factory class to load the corresponding resources from Eclipse. This will avoid conversion errors. The following code shows:

Public static initialcontext getContext () throws namingexception {

IF (InitialContext == NULL) {

//Synchronize

Synchronized (syncobj) {

IF (InitialContext == NULL) {

Hashtable props = new hashtable ();

Props.put (InitialContext.Initial_Context_Factory,

"Org.jnp.interfaces.namingContextFactory");

Props.put (InitialContext.Provider_URL,

"JNP: //127.0.0.1: 1099");

// Use custom initialContextFactoryBuilder

Namingmanager.setInitialContextFactoryBuilder (New

SoftappinitialContextFactoryBuilder ());

InitialContext = New InitialContext (PROPS);

}

}

}

Return INIALCONTEXT;

}

Class SoftappinitialContextFactoryBuilder ImplementsNitialContextFactoryBuilder {

Public InitialContextFactory

CreateInitialContextFactory (Hashtable ENV) throws namingexception {

InitialContextFactory Factory;

String classname = ENV! = NULL?

(String) env.get (Context.Initial_Context_Factory): NULL;

IF (classname == null) {

NOINITIALCONTEXTEXCEPTION NE = New

NOINITIALCONTEXTEXCEPTION

"Need to Specify Class Name in Environment Or System"

"Property, or as an applet parameter, or in AN"

Application Resource File: "

Context.initial_context_factory);

Throw ne;

}

Try {

// Note this sentence, this.getClass (). GetClassLoader () obtained class loader

器 正 类 类 类,, 从 从 从,,,,,, 类 从. 从

// Come with the right class

Factory = (InitialContextFactory)

THIS.GETCLASS (). getClassLoader ().

LoadClass (ClassName) .newinstance ();

} catch (exception e) {

NOINITIALCONTEXTEXCEPTION NE =

New noinitialContextexception

"Cannot Instantiate Class:" ClassName);

Ne.setrootcause (e);

Throw ne;

}

Return Factory;

}

Similar problems exist in the Eclipse environment using hibernate. Hibernate looks up the appropriate class files when resolving * .hbm.xml. When Hibernate itself is inserted into Eclipse as a plugin, then the class file corresponding to * .hbm.xml must be placed in the Hibernate plugin itself (can be loaded through the library), otherwise it will also cause type conversion errors due to the loaded thread. .

Also note that Eclipse's plug-in does not allow loop references.

转载请注明原文地址:https://www.9cbs.com/read-117180.html

New Post(0)