Let the JIVE Forum and Hibernate shared a database connection pool (water) on the virtual host

xiaoxiao2021-03-06  35

Background: On a virtual host (Tomcat) has already running the Jive Forum (using your own database connection pool), now you want to install Hibernate on this virtual host. Question: Because Jive and Hibernate need to use the database connection pool, how Let them share a connection pool. A solution: Because Jive and Hibernate support to find a connection pool instance via JNDI, configure a database connection pool to the virtual host and bind it to JNDI. Since the virtual hosts are prohibited from modifying the Server.xml file, the virtual host configuration database connection pool line is not available by editing server.xml. Then we can write a SERVLET that is started to load, configure the connection pool and bind it to JNDI. Here, I use Tomcat's own DBCP, the database uses mysql. First write a servlet to initialize DBCPPAGE MyDataSource;

Import javax.servlet. *;

Import javax.servlet.http. *;

Import javax.naming. *;

Import org.apache.commons.dbcp. *;

Public class jndidatarouprvlet extends httpservlet {

Public Void Init (ServletConfig Config) Throws ServleTexception

{

String dburl = config.getinitParameter ("dburl");

IF (dburl == null) dburl = "jdbc: mysql: // localhost / jive";

String username = config.getinitParameter ("UserName");

IF (username == null) username = "root";

String password = config.getinitParameter ("password");

IF (Password == Null) Password = "root";

String driverclassname = config.getinitParameter ("driverclassname");

IF (driverclassname == null) DriverclassName = "org.gjt.mm.mysql.driver";

String Maxwaitstr = config.getinitParameter ("maxwait");

IF (MaxWaitstr == Null) maxwaitstr = "3000";

Long maxwait = (new long (maxwaitstr)). LongValue ();

String maxidlestr = config.getinitParameter ("maxidle");

IF (maxidlestr == null) MaxIdlestr = "10";

INT MAXIDLE = (New Integer (Maxidlestr)). INTVALUE ();

String maxactiveStr = config.getinitParameter ("MaxActive");

IF (MaxActiveStr == Null) MaxActiveStr = "100"; int maxactive = (new integer (MaxactiveStr)). INTVALUE ();

String JndinaMe = config.getinitParameter ("JNDINAME");

IF (JNDINAME == NULL) JNDINAME = "My_DataSource";

Try {

CONTEXT INITCTX = (context) new initialContext ();

BasicDataSource BDS = New BasicDataSource ();

BDS.SETURL (DBURL);

BDS.SetUserName (username);

BDS.SETPASSWORD (PASSWORD);

BDS.SetDriverClassName (driverclassname);

BDS.SETMAXWAIT (MAXWAIT);

BDS.SETMAXIDLE (MAXIDLE);

BDS.SETMAXACTIVE (MAXACTIVE);

INitctx.rebind (JNDINAME, BDS);

} catch (exception e) {

E.PrintStackTrace ();

}

} // end init (servletconfig config)

} // end JNDIDATASORVLET

Then, modify the web.xml of the virtual host, similar to the following

Public "- // Sun microsystems, Inc.//dtd Web Application 2.3 // en"

"http://java.sun.com/dtd/web-app_2_3.dtd">

Tomcat Examples

Tomcat Example Servlets and JSP Pages.

jndisource

myDataSource.jndidataSourceServlet

dburl

jdbc: mysql: // localhost / jive

Username

root

Password

root

driverclassname org.gjt.mm.mysql.driver

maxwait

3000

maxidle

10

maxactive

100

JNDINAME

my_datasource

1

Then, the example in the Hibernate document is then attached to here: 1, copy hibernate.hbm.xml and Cat.hbm.xml to the class content. Hibernate.hbm.xml file:

PUBLIC "- // Hibernate / Hibernate Configuration DTD // EN"

"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

my_datasource

True

Net.sf.hibernate.dialect.mysqldiaAlaforct

Cat.hbm.xml file:

Public "- // hibernate / hibernate mapping dtd // en"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

Generated by hibernate with the uuid pattern. ->

Cat.javaPackage mytest;

PUBLIC CLASS CAT {

Private string id;

PRIVATE STRING NAME;

Private char sex;

PRIVATE FLOAT Weight;

Public cat () {

}

Public string getId () {

Return ID;

}

Public void setid (String ID) {

THIS.ID = ID;

}

Public string getname () {

Return Name;

}

Public void setname (String name) {

THIS.NAME = Name;

}

Public char getsex () {

Return SEX;

}

Public void setsex (char sex) {

THIS.SEX = SEX;

}

Public float getWeight () {

Return weight;

}

Public void setWeight (Float Weight) {

THIS.WEIGHT = Weight;

}

}

Hibernate util? .JavaPackage mytest;

Import net.sf.hibernate. *;

Import net.sf.hibs. *;

Public class hibernateutil {

PRIVATE STATIC FINAL SESSIONFACTORY SESSIONFACTORY;

STATIC {

Try {

SessionFactory = new configuration (). CONFIGURE (). BuildSessionFactory ();} catch (hibernateException ex) {

Throw new runtimeException ("Exception Building "Factory: ex.getMessage (), EX);

}

}

PUBLIC Static Final Threadlocal session = new threadlocal ();

Public static session currentsession () throws hibernateException {

Session s = (session) session.get ();

// Open a new session, if this thread HAS NONE YET

IF (s == NULL) {

S = sessionFactory.openSession ();

Session.set (s);

}

Return S;

}

Public static void closesession () THROWS HibernateException {

Session s = (session) session.get ();

session.set (null);

IF (s! = null)

s.close ();

}

}

Finally a test Test.jsp file <% @ Page ContentType = "text / html; charset = GBK"%>

<% @ page import = "Net.sf.hibernate. *"%>

<% @ Page Import = "Mytest. *"%>

<% @ Page Import = "java.sql. *"%>

<% @ Page Import = "javax.naming. *"%>

<% @ Page Import = "org.apache.commons.dbcp. *"%>

<%

Try {

// Test Jiveuser Table First.

CONTEXT INITCTX = New InitialContext ();

Object Obj = (Object) INitctX.lookup ("my_datasource");

Javax.sql.datasource DS = (javax.sql.datasource) OBJ;

Connection conn = ds.getConnection ();

Statement Stmt = conn.createstatement ();

String strsql = "SELECT Userid, Name from Jiveuser";

ResultSet RS = Stmt.executeQuery (strsql);

While (rs.next ()) {

Out.println (Rs.getstring (1));

Out.println (rs.getstring (2));

}

} catch (exception ex) {

EX.PrintStackTrace ();

}

// Test Hibernate.

Cat princess = new cat ();

Princess.setname ("Luck Cat");

Princess.setsex ('f'); Princess.SetWeight (7.4F);

SESSION session1 = hibernateutil.currentSession ();

Transaction tx = session1.begintransaction ();

Session1.save (Princess);

TX.comMit ();

Hibernateutil.closesis ();

%>

To this end, the work configures and test Hibernate is basically completed on the virtual host. The above code is passed on Tomcat. In addition, I just touched Hibernate if there is an unreasonable place in the configuration or code, please ask everyone.

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

New Post(0)