Solutions for the combination of WebLogic 8.1 and Hibernate

xiaoxiao2021-03-06  57

Copyright statement: Convenient learning, this article can be reproduced

Based on Hibernate's advantage in O / R mapping, the current project uses Hibernate entities to replace EJB EntityBean. I made a test in WebLogic 8.1, with EJB SessionBean to call Hibernate's data entities. Because WebLogic and Hibernate provide database connection pools, JNDI, transaction and other functions. Leading ideas still want to use WebLogic Server's high-performance management on these services.

Design ideas:

Use WebLogic database connection pool instead of Hibernate's own connection pool.

Configure Hibernate's sessionFactory under the WebLogic JNDI directory tree.

Call the hibernate entity access database directly in SessionBean

Preparation conditions:

1, install the following software (you can download for free)

1.1 mysql 4.0.21 c: / mysql

Create a database Study, create a data sheet CAT

1.2 mysql-connector-java-3.0.15-ga.zip MySQL driver

1.3 WebLogic Platform 8.1 C: / BEA

WebLogic configuration completed, domain mydomain and server myserver, data pool studyjndi, data source name mysqldatasource

1.4 Hibernate 2.1.2

Refer to other documents to prepare a Hibernate instance CAT, write Cat.hbm.xml and hibernate.cfg.xml files to learn about the basic configuration of Hibernate.

Pay attention to the difference in the database.

2. Create a directory structure

C: / test / lib will copy all files in the lib directory after extracting Hibernate.

C: / TEST / SRC / COM / CHENM Source Code (* .java)

C: / Test / Classes will hibernate profile (hibernate.properties, log4j.properties, cache.ccf)

C: / test / classes / com / chenm compiled code (* .class) Cat.hbm.xml hibernate.cfg.xml

Step 1: Configure Hibernate's environment directory to WebLogic's ClassPath.

Modify WebLogic boot script c: /bea/user_projects/domains/mydomain/startweblogic.cmd, join before @Rem Call WebLogic Server

@Rem Set Hibernate ClassPath

Set hibernate_lib = c: / test / lib

Set hibernate_classes = C: / Test / Classes

Set classpath =% hibernate_lib% / cglib-2.0-rc2.jar;% hibernate_lib% / commons-collections-2.1.jar;% hibernate_lib% / commons-lang-1.0.1.jar;% hibernate_lib% / commons-logging-1.0 .3.jar;% hibernate_lib% / DOM4J-1.4.jar;% hibernate_lib% / hibernate2.jar;% hibernate_lib% / jcs-1.0-dev.jar;% hibernate_lib% / log4j-1.2.8.jar;% hibernate_lib% /odmg-3.0.jar;%Hibernate_Classes% ,%CLASSPATH

Step 2: Modify the hibernat.properties file

2.1 Modify the following

Note Off the MySQL default database connection

## HypersonicsQL

# hibernate.diaract net.sf.hibernate.diaalect.hsqldiaalect # hibernate.connection.driver_class org.hsqldb.jdbcdriver

# Hibernate.Connection.username SA

# hibernate.connection.password

# hibernate.connection.URL JDBC: HSQLDB: HSQL: // localhost

# hibernate.connection.URL JDBC: HSQLDB: TEST

# hibernate.connection.URL JDBC: HSQLDB :.

Use mySQL database

## mysql

Hibernate.Dialev Net.sf.hibernate.dialect.mysqldiaLect

# hibernate.connection.driver_class org.gjt.mm.mysql.driver

Hibernate.Connection.driver_class com.mysql.jdbc.driver

Hibernate.Connection.URL JDBC: MySQL: // localhost: 3306 / study

Hibernate.Connection.username Test

Hibernate.Connection.Password WebLogic

Adjust database query and insert performance parameters

Modify hibernate.jdbc.Fetch_size 50

Modify hibernate.jdbc.batch_size 25

Adjusting Transaction API

# hibernate.transaction.factory_class net.sf.hibernate.transaction.jtatransactionFactory

# hibernate.transaction.Factory_Class Net.sf.hibernate.transaction.jdbcTransActionFactory

for

Hibernate.Transaction.Factory_Class Net.sf.hibernate.Transaction.jtatransActionFactory

Hibernate.Transaction.Factory_class net.sf.hibernate.transaction.jdbctransactionFactory

Use JCS cache

Hibernate.Transaction.Manager_lookup_class net.sf.hibernate.transaction.WebLogictractionsManagerLookup

2.2 Add the following content in the file

Hibernate.Dialev Net.sf.hibernate.dialect.mysqldiaLect

Hibernate.Connection.DataSource Studyjndi / / This is the data connection pool of WebLogic JNDI name

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

Hibernate.Session_Factory_Name Hibernate.Session_Factory // Binds to the name in the WebLogic JNDI directory tree

Step 3. Implement the prerequisite for sessionFactory, create a Startup class using WebLogic T3StartUpDef interface, configured to WebLogic

Automatically run when startup.

3.1 Create a file hibernatestartup.java, compiled into a C: /Test/Classes/com/chenm/Hibernatestartup.class file,

Package com.chenm; 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";

}

}

3.2 Configuring the Startup class

Start the WebLogic console, open the left Mydomain / Deploy / Start and close the node, select the right side "Configure new Startup Class ..."

Fill in the name HibernateStartup, class name com.chenm.hibernatestartup, then click "Create", if there is no error message, it is successful.

Confirmation Success: Turn off WebLogic and restart, observe the information of the DOS window, you can see a lot of line INFO after the WebLogic startup, if not

Error, prove the configuration success. Open the WebLogic console, select Mydomain / Server / MyServer, right click, select the view JNDI tree, if

See Hibernate's JNDI object, you can see the following information on the right:

Binding Name: session_factory

Object Class: Net.sf.hibernate.Impl.SessionFactoryImpl

Object Hatt code: 45706641

Object conversion into strings: net.sf.hibernate.impl.SessionFactoryImpl@2b96d91

Config OK!

4. Write sessionBean Operation Hibernate entity

Define the Remote method in SessionBean

Public void insertcat (String Cat_ID, String Name, Char Sex, Float Weight) {

/ ** @ Todo Complete this method * /

Try {

Context CTX = GetInitialContext ();

SessionFactory sf = (sessionFactory) ctx.lookup ("Hibernate / session_Factory);

Session s = sf.opensession ();

Transaction t = s.begintransaction ();

Cat mycat = new cat ();

mycat.setid; Cat_ID);

mycat.setname (name);

Mycat.setsex (SEX);

Mycat.setWeight (Weight); s.save (mycat);

s.save (mycat);

t.commit ();

s.close ();

}

Catch (Exception EX) {

}

}

Private context getInitialContext () throws exception {string url = "t3: // chenming: 7001"; // chenming server name

String user = NULL;

String password = NULL;

Properties Properties = NULL;

Try {

Properties = new property ();

Properties.put (Context.Initial_Context_Factory, "WebLogic.jndi.wlinitialContextFactory";

Properties.Put (Context.Provider_URL, URL);

IF (user! = null) {

Properties.Put (Context.security_principal, user);

Properties.put (Context.Security_credentials, Password == Null? "": password);

}

Return New InitialContext (Properties);

}

Catch (Exception E) {

Throw e;

}

}

Write tests and run, insert a record in the CAT table

Context context = getInitialContext ();

// Look Up JNDI Name

Object ref = context.lookup ("catsession");

// Look Up Jndi Name and cast to home interface

CatsessionHome = (CatsessionHome) PortableRemoteObject.Narrow (Ref, CatsessionHome.Class);

Catsession = catsessionHome.create ();

Catsession.insertcat ("007", "chenm.cat", '1', 100);

Contact: QQ: 77322113

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

New Post(0)