-->
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.  [ 6 posts ] 
Author Message
 Post subject: On commit,Hibernate reopens and closes additional connection
PostPosted: Tue Jan 02, 2018 12:05 am 
Newbie

Joined: Mon Jan 01, 2018 11:38 pm
Posts: 3
When running the following code in standalone (non JEE) environment with the default Hibernate configuration, I'm seeing the following behavior regarding Hibernate's use of underlying DataSource's connections:

Code:
        Session session = sf.openSession();
        session.getTransaction().begin();
//DataSource::getConnection
//Connection::getAutoCommit
//Connection::setAutoCommit
        Query q = session.createQuery("from User");
        Object res = q.getResultList().get(0);
        session.getTransaction().commit();
//Connection::commit
//Connection::isClosed
//Connection::clearWarnings
//Connection::close
//Connection::getConnection
//Connection::setAutoCommit
//Connection::isClosed
//Connection::clearWarnings
//Connection::close
        session.close();


So, commit() will commit and close the JDBC connection, when it will reopen it, set it to autocommit false and close it immediately.

On further examination, in JdbcResourceLocalTransactionCoordinatorImpl.commit(), in these two lines:
Code:
        jdbcResourceTransaction.commit();
        JdbcResourceLocalTransactionCoordinatorImpl.this.afterCompletionCallback( true );


the first line will commit, close and reopen new connection, and the second line will close the new connection.

Is this expected behavior? How to avoid this additional connection to be created and closed?


(Hibernate 5.2.12, Mysql Connector 6.0.6)


Top
 Profile  
 
 Post subject: Re: On commit,Hibernate reopens and closes additional connection
PostPosted: Tue Jan 02, 2018 4:41 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
Why do you think this line:

Code:
jdbcResourceTransaction.commit();


closes and recreates a new connection?

Are you using the after_statement release mode?

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: On commit,Hibernate reopens and closes additional connection
PostPosted: Tue Jan 02, 2018 5:35 am 
Newbie

Joined: Mon Jan 01, 2018 11:38 pm
Posts: 3
Quote:
Are you using the after_statement release mode?

I'm using the default after_transaction mode

Quote:
Why do you think this line closes and recreates a new connection?

I instrumented DataSource and Connection classes to print when treir methods are called.

Here is the full code:

Code:
package hibernate1;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.util.List;

import javax.sql.DataSource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.dialect.MySQLDialect;

import com.mysql.cj.jdbc.MysqlDataSource;

public class Hibernate1 {
   
    private static DataSource initDataSource() {
        MysqlDataSource ds = new MysqlDataSource();
        ds.setUrl("jdbc:mysql://localhost:3306/db1");
        ds.setUser("root");
        ds.setPassword("password");
        return ds;
    }
   
    private static DataSource instrumentDataSource(final DataSource ds) {
        return (DataSource) Proxy.newProxyInstance(Hibernate1.class.getClassLoader(), new Class[] { DataSource.class }, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("::" + method.getName());
                if (!method.getName().equals("getConnection")) {
                    return method.invoke(ds, args);
                } else {
                    final Connection c = (Connection) method.invoke(ds, args);
                    return (Connection) Proxy.newProxyInstance(Hibernate1.class.getClassLoader(), new Class[] { Connection.class }, new InvocationHandler() {
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            System.out.println(":::" + method.getName());
                            return method.invoke(c, args);
                        }});
                }
            }
        });
    }

    private static SessionFactory initHibernate(DataSource ds) {
        StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
                .applySetting("hibernate.connection.datasource", ds)
                .build();

        Metadata metadata = new MetadataSources(standardRegistry)
                 .addAnnotatedClass( User.class )
                .getMetadataBuilder()
                .build();

        return metadata.getSessionFactoryBuilder().build();
    }

    public static void main(String[] args) throws Exception {
        DataSource ds = instrumentDataSource(initDataSource());
        SessionFactory sf = initHibernate(ds);
       
        Session session0 = sf.openSession();
        System.out.println("# before begin()");
        session0.getTransaction().begin();
        System.out.println("# after begin()");
        List result = session0.createQuery("from User").getResultList();
        System.out.println("# before commit()");
        session0.getTransaction().commit();
        System.out.println("# after commit()");
        session0.close();
    }
}



And here is the full output:
Code:
Jan 02, 2018 5:33:04 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.12.Final}
Jan 02, 2018 5:33:04 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jan 02, 2018 5:33:04 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
::getConnection
:::getMetaData
:::getMetaData
Jan 02, 2018 5:33:04 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
:::close
# before begin()
::getConnection
:::getAutoCommit
:::setAutoCommit
# after begin()
Jan 02, 2018 5:33:05 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
:::prepareStatement
# before commit()
:::commit
:::isClosed
:::clearWarnings
:::close
::getConnection
:::setAutoCommit
:::isClosed
:::clearWarnings
:::close
# after commit()


Top
 Profile  
 
 Post subject: Re: On commit,Hibernate reopens and closes additional connection
PostPosted: Tue Jan 02, 2018 8:15 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
I checked it out and its a bug. Please fill in a new Jira issue for it.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: On commit,Hibernate reopens and closes additional connection
PostPosted: Tue Jan 02, 2018 10:33 pm 
Newbie

Joined: Mon Jan 01, 2018 11:38 pm
Posts: 3
HHH-12197


Top
 Profile  
 
 Post subject: Re: On commit,Hibernate reopens and closes additional connection
PostPosted: Wed Jan 03, 2018 9:11 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
Thanks

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.