Since nature of try-catch-finally blocks, you could put just broker.close() at your finally block (remove it from try block and from catch block).

I used to work in a simpler way:

PersistenceBroker broker = null;
try
{
broker = PersistenceBrokerFactory.defaultPersistenceBroker();

broker.beginTransaction();
broker.store(t_Test);

broker.commitTransaction();
}
catch (Exception e)
{
e.printStackTrace();
try {
broker.abortTransaction();
} catch(Exception e1) {
e1.printStackTrace();
}
}
finally
{
try
{
broker.close();
} catch(Exception e2) {
e2.printStackTrace();
}
}
System.err.println(PersistenceBrokerFactoryFactory.instance().activePersistenceBroker());

Best regards,

Edson Richter


Wang Lei escreveu:

sorry
I paste the wrong code
My fault.

The following code is the right code

MyTest t_Test = new MyTest();

PersistenceBroker broker = null;
try
{
broker = PersistenceBrokerFactory.defaultPersistenceBroker();

broker.beginTransaction();
broker.store(t_Test);

broker.commitTransaction();
broker.close();
}
catch (Exception e)
{
e.printStackTrace();

if (broker != null)
{
if(broker.isInTransaction())
{
broker.abortTransaction();
}
if(!broker.isClosed())
{
broker.close();
}
}

}
finally
{
if ((null != broker)&&(!broker.isClosed()))
{
broker.close();
}

}
System.err.println(PersistenceBrokerFactoryFactory.instance().activePersistenceBroker());

//It keeps increasing and never decreasing.
//just like 1,2,3,and so on



From: [EMAIL PROTECTED]
Reply-To: "OJB Users List" <ojb-user@db.apache.org>
To: "OJB Users List" <ojb-user@db.apache.org>
Subject: Re: a problem about oracle connection leak
Date: Tue, 24 May 2005 10:06:13 -0600

You need to call broker.close() when you are done with each broker. You
are actually asking for 500 simultaneously active brokers.

-steve

Steve Clark
ECOS Development Group
[EMAIL PROTECTED]
(970)226-9291




"Wang Lei" <[EMAIL PROTECTED]>
05/24/2005 09:48 AM
Please respond to
"OJB Users List" <ojb-user@db.apache.org>


To
ojb-user@db.apache.org
cc

Subject
a problem about oracle connection leak






I have a stange a problem.
At first i design a table by the following script
CREATE TABLE MY_TEST
(
TEST_ID INTEGER
)

I also write a class whick is mapping to the table

package junit.entity;

public class MyTest
{
private int testId;
/**
* @return
*/
public int getTestId()
{
return testId;
}

/**
* @param testId */
public void setTestId(int testId)
{
this.testId = testId;
}
}

I write a descriptor in the xml file repository.xml

<class-descriptor class="junit.entity.MyTest" table="MY_TEST">
<field-descriptor autoincrement="true" column="TEST_ID"
jdbc-type="INTEGER" name="testId" primarykey="true"/>
</class-descriptor>

I configure the pool as this
<connection-pool maxActive="20" maxIdle="10" whenExhaustedAction="2"
maxWait="2000" />
this configuration is the same as ojb.properties.

I use the following configuration
ConnectionFactoryClass=org.apache.ojb.broker.accesslayer.ConnectionFactoryPooledImpl



ObjectCacheClass=org.apache.ojb.broker.cache.ObjectCachePerBrokerImpl.


I write a multi-thread class to test ojb and have a strange problem

At first i run the class on my computer on oracle9i in the
company.Everything is fine.
Later i run the class on the computer on oracle9i in the client.I found a
connection leak that the connections increase fast.
after a while, it established more than 200 connections.
I got a exception.

org.apache.ojb.broker.PBFactoryException: Borrow broker from pool failed,
using PBKey org.apache.ojb.broker.PBKey: jcdAlias=oracle,
user=ideal_bright, password=*****
at
org.apache.ojb.broker.core.PersistenceBrokerFactoryDefaultImpl.createPersistenceBroker(Unknown



Source)
at
org.apache.ojb.broker.core.PersistenceBrokerFactoryBaseImpl.defaultPersistenceBroker(Unknown



Source)
at
org.apache.ojb.broker.PersistenceBrokerFactory.defaultPersistenceBroker(Unknown



Source)
at junit.entity.MyTest.main(MyTest.java:86)
Caused by: java.util.NoSuchElementException: Timeout waiting for idle
object
at
org.apache.commons.pool.impl.GenericKeyedObjectPool.borrowObject(GenericKeyedObjectPool.java:782)



... 4 more

I tries many ways ,the result keep the same.

I also run the class on the computer on oracle9i in home.
The same exception happened.

I don't know why.

The following is my code to test ojb.

First this is not a single-thread code.
It result in the same exception.

for (int i = 0; i < 500;i++)
{

try
{
MyTest t_Test = new MyTest();

PersistenceBroker broker =
PersistenceBrokerFactory.defaultPersistenceBroker();
broker.beginTransaction();
broker.store(t_Test);
broker.commitTransaction();

System.out.println(
PersistenceBrokerFactoryFactory.instance().activePersistenceBroker());
//It keeps increasing and never decreasing.
//just like 1,2,3,and so on
}
catch (Exception e)
{
e.printStackTrace();
}
}

The following is a multi-thread code to test ojb.


List t_Threads = new ArrayList();
for (int i = 0; i < 500; i++)
{
Thread t_Thread = new Thread()
{
public void run()
{

MyTest t_Test = new MyTest();

PersistenceBroker broker =
PersistenceBrokerFactory.defaultPersistenceBroker();
broker.beginTransaction();
broker.store(t_Test);
broker.commitTransaction();
System.err.println(
PersistenceBrokerFactoryFactory.instance().activePersistenceBroker());
//It keeps increasing and never decreasing.
//just like 1,2,3,and so on
}
};

t_Threads.add(t_Thread);
}

for (int i = 0; i < t_Threads.size(); i++)
{
Thread t_Thread = (Thread) t_Threads.get(i);
t_Thread.start();
}


Is there anybody can provider a solution to me,
Thanks much.

_________________________________________________________________
享用世界上最大的电子邮件系统— MSN Hotmail。 http://www.hotmail.com


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




_________________________________________________________________
与联机的朋友进行交流,请使用 MSN Messenger: http://messenger.msn.com/cn

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





--
Edson Carlos Ericksson Richter
MGR Informática Ltda.
Fones: 3347-0446 / 9259-2993


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to