Detailed steps in configuring Hibernate on WebLogic JNDI

xiaoxiao2021-03-06  191

First, first you need to put the JAR package and configuration files that Hibernate and the configuration files can search on the ClassPath path that you can search. There are many people who are very confused in single single, in fact, take a closer look at WebLogic Startweblogic.cmd and Startwls.cmd, I think most people know how to configure it.

I have a hibernate project on my machine, in the D: / Test / Oracle directory, the structure in this directory is:

D: / test / oracle / lib places all JAR packets for Hibernate

D: / test / oracle / src placement source code

D: / Test / Oracle / Classes Compile Code and Hibernate Profile (Hibernate.properties, Log4j.properties, Cache.ccf)

Now you need to put the D: / Test / Oracle / Lib directory, those JAR files and d: / test / oracle / classes directory are placed inside WebLogic's classpath, so modify the WebLogic startup script startweblogic.cmd in Mydomain, start WebLogic Before, insert the command to set the classpath, as follows:

@Rem Set Hibernate ClassPath

SET HIBERNATE_LIB = D: / TEST / ORACLE / LIB

SET HIBERNATE_CLASS = D: / TEST / ORACLE / CLASSES

set CLASSPATH =% CLASSPATH%;% HIBERNATE_LIB% / cglib-asm.jar;% HIBERNATE_LIB% / commons-beanutils.jar;% HIBERNATE_LIB% / commons-collections.jar;% HIBERNATE_LIB% / commons-lang.jar;% HIBERNATE_LIB% /commons-logging.jar;%Hibernate_lib%/Dom4j-full.jar;%Hibernate_lib%/Hibernate2.jar;%Hibernate_lib%/JCs.jar;%Hibernate_lib%/log4j-1.2.8.jar;%Hibernate_lib%/odmg .jar;% hibernate_classes%

The following line is the start command inside the script:

@Rem Call WebLogic Server

Call "c: /bea/weblogic700/server/bin/startwls.cmd"

Second, configure the connection pool of the Oracle database on WebLogic. This step is not related to hibernate, but if you want to use EJB, you want to use JTA, you must use the connection pool provided by WebLogic, and you cannot use the Hibernate's connected pool, or other third-party connecting pools, otherwise the container will not Manage database transactions. This step is very simple, just configure the connection pool and TXData Source in WebLogic Console, my TX DataSource takes the name "MyPool"

Third, modify hibernate.properties. Use WebLogic connection pool instead of your own connection pool. I modified D: / Test/oracle/classes/hibernate.properties, increased as follows:

Hibernate.Dialev Net.sf.hibernate.DiaLect.OrglediaLect

Hibernate.Connection.DataSource MyPool

Put

# hibernate.connection.pool_size 1 # Hibernate.Statement_Cache.Size 25

Comment

then

Hibernate.connection.provider_class net.sf.hibernate.connection.DataSourceConnectionProvider

Remove the comment, this will be modified.

In addition, it is

Hibernate.jdbc.Fetch_size 50

Hibernate.jdbc.batch_size 25

There is a great performance impact on database queries and inserts, and the two options can be used to get the best performance.

Fourth, write a Startup class, this class is to implement the weblogic.common.t3startupdef interface. Hibernate gave this class's code snippet, but not all, I completed it, and made some modifications. code show as below:

package com.fankai; import java.util *;. import javax.naming *;. import weblogic.common.T3StartupDef; import weblogic.common.T3ServicesDef; import org.apache.log4j.Logger; import net.sf.hibernate.cfg .Configuration; import net.sf.hibernate.SessionFactory; public class WLSStartup implements T3StartupDef {public static final String SESSION_FACTORY_JNDI = "hibernate"; public static final String URL = "t3: // localhost: 7001"; private static final Logger log = Logger.getLogger (WLSStartup.class); public void setServices (T3ServicesDef services) {} public String startup (String name, Hashtable args) throws Exception {String METHOD_NAME = "startup"; try {log.info (METHOD_NAME "Going to bind hibernate object "); doBind (); log.info (METHOD_NAME ". Bound hibernate object! ");} catch (Exception e) {log.info (METHOD_NAME " Exception while binding hibernate Object to Weblogic JNDI "); e .printStackTrace ();} return "WLS Startup Completed successfully ";} private static void doBind () throws Exception {Properties environment = null; InitialContext context = null; try {environment = new Properties (); environment.put (Context.INITIAL_CONTEXT_FACTORY," weblogic.jndi.WLInitialContextFactory "); environment .put (Context.PROVIDER_URL, URL); log.info ( "Constructing an Initial Directory context object"); context = new InitialContext (environment); Configuration conf = new Configuration () addClass (Cat.class);. SessionFactory sf = Conf.buildsessionFactory (); if (sf == null) throw new exception

SessionFactory can not be built ");! Try {! If (context.lookup (SESSION_FACTORY_JNDI) = null) context.rebind (SESSION_FACTORY_JNDI, sf); else context.bind (SESSION_FACTORY_JNDI, sf);} catch (NamingException nameEx) {context. bind (SESSION_FACTORY_JNDI, sf);}} catch (NamingException nameExp) {throw new Exception ( "NamingException:" nameExp.getMessage ());!} catch (Exception excp) {throw excp;} finally {if (context = null ) {try {context.close (); context = null;} catch (NamingException nameExp) {throw new Exception ( "NamingException for context close:" nameExp.getMessage ());}} environment = null;}}} compiled At this source, you need to pay attention to it, you have to import the WebLogic.jar package and Hibernate all related packages and configuration files. I put this source code in the D: / Test / Oracle / src directory, and have already written early A good Ant script is compiled and compiled, and the compiled class file is placed in the D: / Test / Oracle / Classes directory, which has been added to the ClassPath in WebLogic, so it is very troublesome.

V. Configure the Startup class

Start WebLogic, open the Console console, find Startup & Shutdown on the Applet tree on the left, then click "Configure A New Startup Class ...", please fill in in the name box, fill in the Startup class you written in ClassName , I fill in is com.fankai.wlsstartup, then click "Apply". Then switch to the Target this tab, select "MyServer" in the Avaiable box on the left of Target-Server, click the right arrow, move it to the "chosen" box on the right, and finally click on the "Apply" button. If there is no error message in WebLogic's DOS window, it should have been configured.

6. Now close WebLogic, re-run Startwelogic.cmd, start WebLogic, observe the output information of the DOS window, you can see the scroll output of Hibernate's initialization information, which proves that it has been configured. Now open the Console console, click Servers | MyServer in the left Applet tree, then find "View JNDi Tree" on the right side, click on it, open a browser window, display the JNDI tree, then you can see it A JNDI object named hibernate, click on it on the Applet tree on the left, look at the details on the right, the information on my machine is as follows: bind name: hibernate

Object class: net.sf.hibernate.Impl.SessionFactoryImpl

Object Hash Code: 454492

Object to string: Net.sf.hibernate.impl.SessionFactoryImpl@6ef5c

Completely correct!

Finally, you can use JND to find sessionFactory with JND in EJB or Servlet / JSP.

E.g:

Context ctx = new initialContext (); sessionFactory sf = (sessionFactory) CTX.lookup ("hibernate");

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

New Post(0)