Questions about Hibernate using external connection

xiaoxiao2021-03-06  82

I have recently tried Hibernate in a project. Because the project is divided into two parts, in another part, a connection pool (self-developed) has been used. Now two parts should be combined, so there is a problem. Start, we decided to use Hibernate's external connection method for data access:

Java code:

1

2

public

Static DBSession OpenSession

(

)

{

...}

3 dBSession dbssion =

NULL;

4

Try

{

...}

5

//System.out.println ("Session is start ");

6 ConnectionPool ConnectionPool = PooledConnectionFactory.

GetConnectionPool

(

);

Seduce

Connection con = ConnectionPool.

GetConnection

(

);

8

9 session s =

(Session

Sense.

get

(

);

10

IF

(s ==

NULL

)

{

...}

11 s = fact.

OpenSession

(Con

);

// Here, use the connection given by the external connection pool

12 session.

set

(s)

);

13

}

14

15 dBSession =

New dbssion

(s)

);

16

}

Catch

(

Throwable T

)

{

In one

String msg = "COULDN

'T Open a hibernate session for sessionFactory' " "

'"; 18 log.error (MSG, T); 19 throw new dbexception (msg ": " TgetMessage ()); 20} 21 Return DBSession; 22} 23

The session method is turned off as follows:

Java code:

1

2

public

Static

Void CloseSession

(

)

Throws HibernateException,

EXCEPTION

{

...}

3 session s =

(Session

Sense.

get

(

);

4 session.

set

(

NULL

);

5

IF

(s! =

NULL

)

{

...}

6 s.

Connection

(

).

Close

(

);

7 s.

Close

(

);

8

SYSTEM.

OUT.

PRINTLN

("session closing"

);

9

// s = NULL;

10

11

}

12

}

13

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

New Post(0)