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 =% hibernate_lib% / cglib-asm.jar;% hibernate_lib% / commons-beanutils.jar;
% Hibernate_lib% / commons-collections.jar;% hibernate_lib% / commns-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%;% classpath%
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 TXDataSource 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
Hibernate.connection.Provider_class net.sf.hibernate.Connection.DataSource.Session_Factory_Name Hibernate.Session_Factory
Note the last line, this is the name that uses hibernate to bind JNDI to JNDi. It should be hibernate / session_factory, but WebLogic requires to change to. Number, but when Lookup is loop, you still have to write hibernate / session_factory
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.
In order to ensure the pre-construction of the sessionFactory instance, create a Startup class using WebLogic T3StartUpDef interface, running when WebLogic is started:
Package com.fankai;
Import java.util.hashtable;
Import WebLogic.common.t3startupdef;
Import WebLogic.common.t3ServicesDef;
Import Net.sf.hibiBernate.cfg.configuration;
Import Net.sf.hibActory;
Public Class HibernateStartup Implements T3StartUpdef {
Public void setservices (T3ServicesDef Services) {}
Public String Startup (String Name, Hashtable Args) Throws Exception {
Configuration conf = new configuration (). Addclass (catch);
SessionFactory sf = conf.buildsessionFactory ();
Return "Hibernate Startup Completed SuccessFully";
}
}
The code is very simple, but it is to ensure pre-running
Configuration conf = new configuration (). Addclass (catch);
SessionFactory sf = conf.buildsessionFactory ();
Create sf, and Hibernate will call a range of ways to bind the sf to the Hibernate / Session_Factory path under WebLogic JNDI tree.
4, compile hibernatestartup.java
When compiling this source code, you need to pay attention to importing WebLogic.jar packages and Hibernate all related packages and profiles. I put this source code in the D: / Test / Oracle / src directory, and I have compiled it with the Ant script that has already prepared it early and compiled. The compiled class file is placed to D: / Test / Oracle / Under the class directory, this directory has been added to the ClassPath of WebLogic, so it is very much.
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 of 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 / session_Factory);
How to configure on other app servers, in nature, and WebLogic is the same, just you have to make sure before running in other programs
Configuration conf = new configuration (). Addclass (catch);
SessionFactory sf = conf.buildsessionFactory ();
These two lines of code is OK.