-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 
Author Message
 Post subject: How to close a session correctly?
PostPosted: Fri Feb 09, 2007 8:37 am 
Newbie

Joined: Fri Oct 07, 2005 11:53 am
Posts: 9
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 2.0

Name and version of the database you are using: Mysql 5.0


I have a lot of problems with connections and Hibernate. I think the problem is on how I close the sessions of hibernate.

This is my way:
session.connection().close()
session.close();

I tried several combinations here.. but I need someone tell me how is the correct way.

Thanks
d2clon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 09, 2007 9:31 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
you don't want to close the connection by yourself. session.close() works just fine in 99% of cases

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 09, 2007 11:51 am 
Regular
Regular

Joined: Tue Nov 04, 2003 12:37 pm
Posts: 57
just use session.close();


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 18, 2007 10:23 am 
Newbie

Joined: Fri Oct 07, 2005 11:53 am
Posts: 9
Dear friends:

Now I only make this sentences at the end of my request:

session.flush();
session.close();


All my sequence is:

Session s = sessions.openSession();
s.load( class, id ); // or whatever hibernate action
s.flush();
s.close();

But I still have a lot of problems with lost connections and Broken Pipe errors.

I use c3p0 pool connection, and Mysql database.

There is no problem if I just started the application, but withing few hours the connections start to return me problems like:

java.net.SocketException: Broken pipe

and

net.sf.hibernate.util.JDBCExceptionReporter(58) - No operations allowed after connection closed.Connection was implicitly closed du
e to underlying exception/error:

or

net.sf.hibernate.transaction.JDBCTransaction(68) - Commit failed
com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state.

I don't know what I do wrong...


-------------
007-02-18 14:58:44,288 [TP-Processor7] WARN net.sf.hibernate.util.JDBCExceptionReporter(57) - SQL Error: 0, SQLState: 08S01
2007-02-18 14:58:44,289 [TP-Processor7] ERROR net.sf.hibernate.util.JDBCExceptionReporter(58) - Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: Broken pipe

STACKTRACE:

java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2637)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1554)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3176)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1153)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1266)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:89)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:880)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:273)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:941)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:961)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:413)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2131)
at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:2001)
at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1930)
at com.us3.hibernate.HibernateSupport.load(HibernateSupport.java:646)
-----------------


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 18, 2007 11:40 am 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
I have faced this problem with MySQL and hibernate connection pool. Swtching to C3P0 has solved this problem for me. Please post the hibernate configuration file.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 5:40 am 
Newbie

Joined: Fri Oct 07, 2005 11:53 am
Posts: 9
thx for the help in advance.. this problem is driving me crazy..

This is my hibernate.cfg.xml:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<!-- Generated file - Do not edit! -->

<hibernate-configuration>

        <!-- a SessionFactory instance listed as /jndi/name -->
        <session-factory>

                <!-- properties -->
                <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
                <property name="show_sql">false</property>
                <property name="use_outer_join">true</property>
                        <property name="connection.username">sici</property>
                        <property name="connection.password">sici</property>
                        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
                        <property name="connection.url">jdbc:mysql://database:3306/sici?autoReconnect=true</property>
                        <property name="connection.pool_size">20</property>

                <property name="c3p0.min_size">10</property>
                <property name="c3p0.max_size">100</property>
                <property name="c3p0.timeout">10</property>
                <property name="c3p0.acquireRetryAttempts">30</property>
                <property name="c3p0.acquireIncrement">5</property>
                <property name="c3p0.idleConnectionTestPeriod">300</property>
                <property name="c3p0.initialPoolSize">20</property>
                <property name="c3p0.maxPoolSize">100</property>
                <property name="c3p0.maxIdleTime">300</property>
                <property name="c3p0.maxStatements">50</property>
                <property name="c3p0.minPoolSize">10</property>

                <!-- mapping files -->
                <mapping resource="com/sici/beans/catalogues/CatSectorBean.hbm.xml"/>
                <mapping resource="com/basic/beans/UserBean.hbm.xml"/>
               
        </session-factory>

</hibernate-configuration>



Any suggestion is welcome.


Top
 Profile  
 
 Post subject: i have the same problem
PostPosted: Mon Feb 26, 2007 12:49 pm 
Newbie

Joined: Mon Feb 26, 2007 12:46 pm
Posts: 3
hi all, i have the same problem and i cant resolved by now , any ideas?

this is my hibernate.cfg

<hibernate-configuration>
<session-factory>
<property name="hibernate.c3p0.max_size">750</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">luisxx12321</property>
<property name="hibernate.connection.url">jdbc:mysql://172.31.210.254:3306/moto</property>
<property name="hibernate.connection.username">lwong</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>

<property name="connection.aggressive_release">true</property>
<property name="connection.autoReconnect">true</property>
<property name="connection.autoReconnectForPools">true</property>
<property name="connection.is-connection-validation-required">true</property>
<property name="dbcp.maxActive">400</property>
<property name="dbcp.whenExhaustedAction">1</property>
<property name="dbcp.maxWait">120000</property>
<property name="dbcp.maxIdle">1</property>
<property name="dbcp.minIdle">0</property>
<property name="dbcp.ps.maxActive">10</property>
<property name="dbcp.ps.whenExhaustedAction">1</property>
<property name="dbcp.ps.maxWait">120000</property>
<property name="dbcp.ps.maxIdle">100</property>
<property name="dbcp.validationQuery">select 1</property>
<property name="dbcp.testOnBorrow">true</property>
<property name="dbcp.testOnReturn">false</property>

<mapping resource="com/moto/efectivo/objectbusiness/Address.hbm.xml"/>
...
</session-factory>
</hibernate-configuration>

and this my session close

public class HibernateUtil {
public static final SessionFactory sessionFactory;
public static final ThreadLocal threadSession = new ThreadLocal();
static
{
try
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex)
{
throw new ExceptionInInitializerError(ex);
}
}

public static Session currentSession() throws HibernateException
{
Session session = (Session) threadSession.get( );
if (session == null)
{
session = sessionFactory.openSession();
threadSession.set( session );
}
return session;
}

public static void closeSession() throws HibernateException
{
Session session = (Session) threadSession.get( );
if ( session != null )
{
session.close();
}
threadSession.set(null);
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 26, 2007 1:31 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://hibernate.org/42.html

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 28, 2007 4:38 am 
Newbie

Joined: Fri Oct 07, 2005 11:53 am
Posts: 9
christian wrote:
http://hibernate.org/42.html


I've read the document, Christian, but I am not using transactions and not even shared Hibernate Sessions between request petitions.

I think this is not the problem because if I was using sessions between request petitions without persist and load my session correctly I had the problem inmediatly.

The problem only happen when the application is running more than 8 hours.

Please Christian, tell us which you think is the problem.

Thx.
d2clon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 28, 2007 8:05 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The problem is that you are ignoring the advice on that page and think you can do stuff "without transactions". Follow the patterns on this page, use the code from this page. Do not use anything else.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Solved
PostPosted: Thu Mar 01, 2007 7:04 pm 
Newbie

Joined: Mon Feb 26, 2007 12:46 pm
Posts: 3
i solved my problem., changing the hibernate.cfg like this

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<!-- Use a Tomcat 5.5 JNDI datasource -->

<property name="connection.datasource"> java:comp/env/jdbc/MjPool</property>

<property name="show_sql">true</property>
<property name="dialect"> org.hibernate.dialect.MySQLDialect </property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class"> org.hibernate.cache.NoCacheProvider </property>



<mapping resource="com/moto/efectivo/objectbusiness/ImeiAsig.hbm.xml"/>
.....
.....
</session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject: Re: Solved
PostPosted: Sat Mar 03, 2007 7:51 am 
Newbie

Joined: Fri Oct 07, 2005 11:53 am
Posts: 9
lwong1980 wrote:
i solved my problem., changing the hibernate.cfg like this

<property name="connection.datasource"> java:comp/env/jdbc/MjPool</property>


Plis, lwong1980, could you show your <Resource/> configuration?

Thx.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 03, 2007 2:17 pm 
Newbie

Joined: Mon Feb 26, 2007 12:46 pm
Posts: 3
Sure, here you are.

META-INF/Context.xml or put in place CATALINA_HOME/conf/catalina/Moto.xml


<?xml version="1.0" encoding="UTF-8"?>
<Context path="/Moto">
<Resource
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
maxActive="0"
maxIdle="5"
maxWait="5000"
minEvicatableIdleTimeMillis="60000"
minEvictableIdleTimeMillis="6000"
name="jdbc/MjPool"
password="123"
removeAbandoned="true"
removeAbandonedTimeout="60"
testOnBorrow="true"
testWhileIdle="true"
timeBetweenEvictionRunsMillis="10000"
type="javax.sql.DataSource"
url="jdbc:mysql://172.31.210.254:3306/moto?autoReconnect=true"
username="luis"
validationQuery="select 1" />
</Context>

also add to web.xml

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/MjPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

and thats all.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 10, 2007 6:28 am 
Newbie

Joined: Fri Oct 07, 2005 11:53 am
Posts: 9
lwong1980:

Thankyou very much¡

Now my application is not falling down every day.

:)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.