Configure hibernate for jndi_jsp programming on WebLogic

Source: Internet
Author: User
Tags bind connection pooling serialization 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:estoracle directory, the structure of the directory is:

D:estoraclelib all jar packs that place hibernate
D:ESTORACLESRC Place Source Code
D:estoracleclasses compiled code and Hibernate configuration file (Hibernate.properties, log4j.properties, CACHE.CCF)

Now you need to put all the jar files and d:estoracleclasses directories in the D:estoraclelib 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:

@rem Set Hibernate classpath
Set Hibernate_lib=d:estoraclelib
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 start command in the original script:

@rem Call Weblogic Server
Call "C:eaweblogic700serverinstartwls.cmd"

Second, configure the Oracle database connection pool on the WebLogic, this step is not related to hibernate, but if you want to use EJB, want to use JTA, then you must use the connection pool provided by WebLogic, instead of using hibernate from the connection pool, or other Third-party connection pooling, the container will not be able to manage database transactions. This step is very simple, that is, in the WebLogic console Configuration connection pool and TxData Source, my txdatasource name is called "Mypool"

Third, modify the hibernate.properties. Use a WebLogic connection pool instead of a self-contained connection pool. I modified the d:estoracleclasseshibernate.properties to add the following line:

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.

The other thing to mention is that

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:

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, is to ensure that the pre-run

Sessionfactory SF = new Configuration (). Configure (). 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:ESTORACLESRC directory, with the long written ant script to run the compile, and the compiled class file is placed in the D:estoracleclasses directory, The catalogue has been added to the classpath of WebLogic, so it is very convenient.

V. Configuration of the Startup class

Start WebLogic, open the console console, locate startup & Shutdown on the Left applet tree, and click "Configure a new startup Class ..." On the right, and fill in the Name box. Fill in the classname inside you write the startup class, I fill in is Com.javaeye.HibernateStartUp, then click "Apply". Then switch to target this tab, select "MyServer" in the Avaiable box on the left of Target-server, click on the right arrow, move it to the "Chosen" box on the right, and then click the "Apply" button at the end. If there is no error message in the WebLogic DOS window at this time, it should have been configured successfully.

Six, now close WebLogic, then rerun Startwelogic.cmd, start WebLogic, observe the output of the DOS window, you can see the hibernate initialization information of a screen of scrolling output, proof has been configured successfully. Now open the console console, click on the Servers|myserver in the Left applet tree, and then find "View JNDI tree" on the bottom right, and click on it to open a browser window showing the Jndi At this point you can see a Jndi object called Hibernate, click it in the applet tree on the left, and look at the details on the right, and 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

Absolutely right!

Finally, you can use Jnd lookup in EJB or servlet/jsp to get sessionfactory.

For example:

Context ctx = new InitialContext ();
Sessionfactory SF = (sessionfactory) ctx.lookup ("Hibernate/session_factory");

Please note that the above code can only be run within the WebLogic container and not outside the WebLogic container. Because Sessionfactory does not implement a serialization interface, the attempt to serialize Sessionfactory to the local is certainly unsuccessful when the client program (running in another separate JVM) accesses WebLogic JNDI. However, even if sessionfactory implements the serialization interface, it cannot be called normally outside the WebLogic container because it is not an object that can support RMI.

  In contrast, WebLogic's own datasource,ejb,jms, and so on, are supported by RMI (provided that the corresponding Jar is WebLogic), so you can take a lookup outside of 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.