Configure hibernate as Jndi on WebLogic

Source: Internet
Author: User
Tags bind oracle database log4j

First, you need to put the jar package and configuration files used by hibernate on the classpath path that WebLogic can search. This step alone a lot of people are confused, in fact, to take a closer look at WebLogic startup script files Startweblogic.cmd and startwls.cmd, I think most people know how to configure.

I have a hibernate project on my machine, in the D:\test\oracle directory, the structure of the directory is:

Java code:

D:\test\oracle\lib 放置hibernate的所有jar包
D:\test\oracle\src 放置源代码
D:\test\oracle\classes 编译好的代码和hibernate的配置文件(hibernate.properties, log4j.properties, cache.ccf)

Now you need to put all the jar files and D:\test\oracle\classes directories in the D:\test\oracle\lib directory into WebLogic Classpath, So modify the mydomain inside the WebLogic startup script Startweblogic.cmd, before starting WebLogic, insert the command to set Classpath, as follows:

Java code:

@rem set hibernate classpath
set HIBERNATE_LIB=D:\test\oracle\lib
set HIBERNATE_CLASSES=D:\test\oracle\classes
set 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%;%CLASSPATH%

The following line is the start command in the original script:

Java code:

@rem Call Weblogic Server
call "C:\bea\weblogic700\server\bin\startWLS.cmd"

Second, configure the Oracle database connection pool on the WebLogic. This step would have nothing to do with hibernate, but if you want to use an EJB and want to use JTA, you must use the connection pool provided by WebLogic instead of using Hibernate's own connection pool or other Third-party connection pools, or the container will not be able to manage database transactions. This step is very simple, is in the WebLogic console Configuration connection pool and TxData Source, my tx datasource named "Mypool"

Third, modify the hibernate.properties. Use a WebLogic connection pool instead of a self-contained connection pool. I modified the D:\test\oracle\classes\hibernate.properties to add the following line:

Java code:

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

Note that the last line, which uses Hibernate to bind Jndi to Jndi names, should have been hibernate/session_factory, but the weblogic requirement was changed to. , but you still have to write hibernate/session_factory in the program.

In addition, mention is made of:

Java code:

hibernate.jdbc.fetch_size 50
hibernate.jdbc.batch_size 25

There is a great performance impact on database queries and inserts, and tuning these options can achieve the best performance.

To ensure the sessionfactory of the instance, create a startup class using the WebLogic T3startupdef interface to run when the WebLogic is started:

Java code:

package com.fankai;

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 {
Configuration conf = new Configuration().addClass(Cat.class);
SessionFactory sf = conf.buildSessionFactory();
return "Hibernate Startup completed successfully";
}
}

The code is very simple, in fact, is to ensure that the pre-run

Java code:

Configuration conf = new Configuration().addClass(Cat.class);
SessionFactory sf = conf.buildSessionFactory();

The SF is created, and hibernate invokes a series of class methods that bind SF to the hibernate/session_factory path under WebLogic's Jndi tree.

4, compile Hibernatestartup.java

When compiling this source code, it is necessary to import the Weblogic.jar package and hibernate all the related packages and configuration files. I put this source code in the D:\TEST\ORACLE\SRC directory, with the long written ant script to run the compile, and the compiled class file is placed in the D:\test\oracle\classes directory, The catalogue has been added to the classpath of WebLogic, so it is very convenient.

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.