i'm using hibernate3.
is my dao using the right way to retrieve object from mysql db ? i don't close connection, and i use hibernatesessionfactory.currentSession() to always get current session
Code:
public MyMember getMyMember(String name) {
MyMember MyMember = new MyMember();
try {
Session sess = HibernateSessionFactory.currentSession();
sess.clear();
HibernateSessionFactory.beginTransaction();
MyMember = (MyMember)sess.get(MyMember.class, name);
HibernateSessionFactory.commitTransaction();
} catch(HibernateException e) {
//placeholder
System.out.println("There was a problem retrieving the MyMember: " + name + " : " + e.getMessage());
HibernateSessionFactory.rollbackTransaction(); //i get exception on this line
}
return MyMember;
}
sometimes, i will get error like below
Code:
0:53:20 PM com.mchange.v2.c3p0.impl.NewPooledConnection handleThrowable
WARNING: [c3p0] Another error has occurred [ com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state. ] which will not be reported to listeners!
com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:888)
at com.mysql.jdbc.Connection.getMutex(Connection.java:3640)
at com.mysql.jdbc.Connection.rollback(Connection.java:5088)
at com.mchange.v2.c3p0.impl.NewProxyConnection.rollback(NewProxyConnection.java:855)
at org.hibernate.transaction.JDBCTransaction.rollbackAndResetAutoCommit(JDBCTransaction.java:183)
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:162)
at com.theor.Member.model.HibernateSessionFactory.rollbackTransaction(HibernateSessionFactory.java:125)
at com.theor.Member.model.MyMemberDao.getMyMember(MyMemberDao.java:57)
at com.theor.Member.model.HttpMember.service(HttpMember.java:255)
at $IEngineService_11c3795d1c9.service($IEngineService_11c3795d1c
my hibernate.cfg.xml configuration
Code:
<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>
<!-- Transaction isolation 2 = READ_COMMITTED -->
<property name="connection.isolation">2</property>
//my hibernate factory look like this
Code:
public static Session currentSession() throws NestedRuntimeException {
Session session = (Session) threadSession.get();
try {
if (session == null) {
session = sessionFactory.openSession();
threadSession.set(session);
}
} catch(HibernateException e) {
throw new NestedRuntimeException("There was a problem retrieving the current session",e);
}
return session;
}
public static void commitTransaction() throws NestedRuntimeException {
try {
Transaction tx = (Transaction)threadTransaction.get();
if(tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.commit();
threadTransaction.set(null);
} catch (HibernateException e) {
}
}
/**
* Rollback a hibernate transaction
*
* @throws NestedRuntimeException
*/
public static void rollbackTransaction() throws NestedRuntimeException {
try {
Transaction tx = (Transaction)threadTransaction.get();
threadTransaction.set(null);
if(tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.rollback();
closeSession();
} catch (HibernateException e) {
throw new NestedRuntimeException("There was a problem rolling back the transaction", e);
}
}