Solutions for the combination of WebLogic 8.1 and Hibernate

xiaoxiao2021-03-06  106

Copyright Notice: Convenient to learn, this article can reprint the advantage based on Hibernate in O / R mapping. At present, Hibernate entities replace EJB ENTITYBEAN, I have made a test in WebLogic 8.1, with EJB SessionBean call hibernate. 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. Create the following software in SessionBean: 1. Install the following software (you can download free download) 1.1 mysql 4.0.21 c: / mysql Create a database Study, create a data table cat1.2 mysql-connector-java- 3.0.15-ga.zip MySQL Driver 1.3 WebLogic Platform 8.1 C: / Bea WebLogic Configuration Completion, Domain MYDOMAIN and Server MyServer, Data Pool Studyjndi, Data Source Name MysqlDataSource 1.4 Hibernate 2.1.2 Refers to other documents Write 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 to copy all files in the lib directory after the Hibernate decompressed to this C: / TEST / SRC / COM / CHENM source code storage (* .java) C: / test / classes Hibernate's profile (hibernate.properties, cache.ccf) C: / Test / Classes / COM / CHENM compiled code (* .class) Cat.hbm.xml Hibernate.cfg.xml Step 1 : Configure the environment of the Hibernate to the ClassPath in WebLogic.

Modify Weblogic startup script C: /bea/user_projects/domains/mydomain/startweblogic.cmd, added 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 hibernat.properties 2.1 Modify the following file to comment out the default mysql database connection ## HypersonicSQL # hibernate.dialect net.sf.hibernate.dialect.HSQLDialect # 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.dialect 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 parameter modification hibernate.jdbc.fetch_size 50 hibernate.jdbc.batch_size 25 modified adjusted Transaction API #hibernate. Transaction.factory_class net.sf.hibernate.transaction.jtatransactionFactory # hibernate.tr ansaction.factory_class net.sf.hibernate.transaction.JDBCTransactionFactory use JCS cache hibernate.transaction.manager_lookup_class net to hibernate.transaction.factory_class net.sf.hibernate.transaction.JTATransactionFactory hibernate.transaction.factory_class net.sf.hibernate.transaction.JDBCTransactionFactory .sf.hibernate.transaction.WeblogicTransactionManagerLookup2.2 add the following hibernate.dialect net.sf.hibernate.dialect.MySQLDialect hibernate.connection.datasource studyjndi // here in the end of the file is weblogic data connection pool JNDI name hibernate.connection. Provider_class net.sf.hibernate.connection.datasource.Session_Factory_name Hibernate.Session_Factory // Binds to the name of the WebLogic JNDI directory tree Step 3. Implement the prerequisite for sessionFactory,

Create a Startup class using WebLogic T3StartUpDef interface, configure it to run automatically when WebLogic is started. 3.1 Create a file HibernateStartUp.java, and compiled into 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.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";}} 3.2 startUp class start Weblogic console configuration, left open mydomain / deployment / start And close the node, select the right side "Configure new Startup Class ..." fill in the name HibernateStartup, class name com.cher.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 WebLogic startup, if there is no error, prove the configuration success.

Open the WebLogic console, select Mydomain / Server / MyServer, right click, select the view JNDI tree, if you see Hibernate's JNDI object, you can see the following information on the right: Bind Name: session_factory object class: Net.sf.hibernate. impl.SessionFactoryImpl Object hash Code: 45706641 object into a string:! net.sf.hibernate.impl.SessionFactoryImpl@2b96d91 Config OK 4 SessionBean write operation SessionBean Hibernate entities defined Remote method 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 (name); mycat.setsex; mycat.setweight (weight); s .save (mycat); ssave (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; property proties = NUL l; try {properties = new Properties (); 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;}} and write tests Run, insert a record in the CAT table CONTEXT = GetInitialContext (); // Look Up JNDi Name Object Ref = Context.lookup ("CatSession");

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

New Post(0)