Configure Hibernate as JNDI_MySQL on Weblogic

Source: Internet
Author: User
1. First, you need to put the jar packages and configuration files used by Hibernate on the CLASSPATH path that Weblogic can search. Many people are confused about this step. In fact, let's take a closer look at the startup script files startWeblogic. cmd and startWLS. cmd of Weblogic. I think most people know how to configure it. There is a Hibernate project on my machine, HibernateWebLogicJNDI

  

1. First, you need to put the jar packages and configuration files used by Hibernate on the CLASSPATH path that Weblogic can search. Many people are confused about this step. In fact, let's take a closer look at the startup script files startWeblogic. cmd and startWLS. cmd of Weblogic. I think most people know how to configure it.

There is a Hibernate project on my machine. under the D: estoracle directory, the structure of this directory is:

D: estoraclelib: Place all the jar packages of hibernate.
D: put source code in estoraclesrc
D: the compiled code of estoracleclasses and the configuration file of hibernate (hibernate. properties, log4j. properties, cache. ccf)

Now we need to place the jar files and the D: estoracleclasses directories under the D: estoraclelib directory into the Weblogic CLASSPATH. Therefore, we need to modify the Weblogic startup script startWeblogic in mydomain. cmd: before Weblogic is started, insert the CLASSPATH setting command as follows:

@ Rem set hibernate classpath
Set HIBERNATE_LIB = D: estpoliclelib
Set HIBERNATE_CLASSES = D: estoracleclasses
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_LIB % jta. jar; % HIBERNATE_CLASSES %;

The following line is the startup command in the script:

@ Rem Call Weblogic Server
Call "C: eaweblogic700serverinstartWLS. cmd"

2. configure the connection pool of the Oracle database on Weblogic. this step has nothing to do with Hibernate. but if you want to use EJB and JTA, you must use the connection pool provided by Weblogic, instead, you cannot use the connection pool that comes with Hibernate or other third-party connection pools. Otherwise, the container cannot manage database transactions. This step is very simple, that is, configuring the Connection Pool and TxData Source in the Weblogic Console. my TxDataSource is called "mypool"

3. modify hibernate. properties. Use the Weblogic connection pool instead of the built-in connection pool. I modified D: estoracleclasseshib.pdf. properties and added the following lines:

Hibernate. dialect net. sf. hibernate. dialect. OracleDialect
Hibernate. connection. datasource mypool
Hibernate. connection. provider_class net. sf. hibernate. connection. performanceconnectionprovider
Hibernate. session_factory_name hibernate. session_factory

Note that the last line is the name bound to JNDI by Hibernate. it should have been hibernate/session_factory, but Weblogic must be changed. but you still need to write hibernate/session_factory during lookup in the program.

What's more

Hibernate. jdbc. fetch_size 50
Hibernate. jdbc. batch_size 25

These two options have a significant impact on the performance of database queries and inserts, respectively. tuning these two options can achieve the best performance.

To guarantee the pre-creation of the SessionFactory instance, use the T3StartUpDef interface of Weblogic to create a StartUp class and run it at Weblogic StartUp:

Package com. javaeye;

Import java. util. Hashtable;
Import weblogic. common. T3StartupDef;
Import weblogic. common. T3ServicesDef;
Import net. sf. hibernate. cfg. Configuration;
Import net. sf. hibernate. SessionFactory;

Public class HibernateStartUp implements T3StartupDef {

Public void setServices (T3ServicesDef services ){}

Public String startup (String name, Hashtable args) throws Exception {
SessionFactory sf = new Configuration (). configure (). buildSessionFactory ();
Return "Hibernate Startup completed successfully ";
}
}

The code is very simple. In fact, it is to ensure that the code is run in advance.

SessionFactory sf = new Configuration (). configure (). buildSessionFactory ();

Create sf, and Hibernate will call a series of class methods to bind sf to the hibernate/session_factory path under the JNDI tree of Weblogic.

4. compile HibernateStartUp. java

When compiling this source code, you must note that you need to import the weblogic. jar package and all the relevant Hibernate packages and configuration files. I put the source code under the D: estoraclesrc directory, and compiled it by running the ant script that has already been compiled, and the compiled class file is placed under the D: estoracleclasses directory, this directory has been added to Weblogic's CLASSPATH, which saves a lot of trouble.

5. configure the StartUp class

Start Weblogic, open the Console, find StartUp & Shutdown in the Applet tree on the left, and click "Configure a new Startup Class..." on the right... in the Name box, enter the StartUp class you wrote in ClassName, and I entered com. javaeye. hibernateStartUp, and then click "Apply ". Switch to the Target tab, select "myserver" in the Avaiable box on the left of Target-Server, and click the right arrow to move it to the "Chosen" box on the right, click "Apply. If no error message is displayed in the DOS window of Weblogic, the configuration is successful.

6. close Weblogic now, run startWelogic. cmd again, start Weblogic, observe the output information of the DOS window, and you can see the rolling output of Hibernate initialization information on a screen, proving that the configuration has been successful. Open the Console now, click Servers | myserver in the Applet tree on the left, and then click View JNDI tree at the bottom of the right to open a browser window, display the JNDI tree. then you can see a JNDI object named hibernate. click it in the Applet tree on the left to view the detailed information on the right. The information on my machine is as follows:

Bind Name: hibernate
Object Class: net. sf. hibernate. impl. SessionFactoryImpl
Object hashCode: 454492
Object To String: net. sf. hibernate. impl. SessionFactoryImpl @ 6ef5c

Completely correct!

Finally, you can use JND lookup in EJB or Servlet/JSP to obtain SessionFactory at will.

For example:

Context ctx = new InitialContext ();
SessionFactory sf = (SessionFactory) ctx. lookup ("hibernate/session_factory ");

Note: The above code can only run in the WebLogic container, but not outside the WebLogic container. Because SessionFactory does not implement the serialization interface, when the client program (running in another separate JVM) remotely accesses WebLogic JNDI, attempting to serialize SessionFactory to a local machine will certainly fail. However, even if SessionFactory implements a serialized interface, it cannot be called outside the WebLogic container because it is not an object that supports RMI.

In contrast, WebLogic DataSource, EJB, JMS, and so on all support RMI (the premise is that the corresponding jar of WebLogic should be available), so you can look up outside WebLogic, and use it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.