Branislav Lazic
Branislav Lazic

Reputation: 14806

Hibernate returns "weird" connection

I'm using JPA in one project with Hibernate and I had a need to perform some operations on pure JDBC level so I used this method to obtain connection to my database:

public class ConnectionUtil {
    public static EntityManagerFactory EMF = Persistence
        .createEntityManagerFactory("persistenceUnitName");

    public static Connection getConnection() throws SQLException {

        Connection connection = null;
        EntityManager em = null;
        try {
            em = EMF.createEntityManager();
            Session session = (Session)EMF.createEntityManager()
                    .getDelegate();
            SessionFactoryImplementor sfi = (SessionFactoryImplementor) session
                    .getSessionFactory();
            @SuppressWarnings("deprecation")
            ConnectionProvider cp = sfi.getConnectionProvider();
            connection = cp.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null) {
                em.close();
            }
        }

        return connection;
    }
}

And I used this method for insertion of records into my database:

public void saveOrUpdate(String sqlStatement, Connection connection,  Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {

            preparedStatement = connection.prepareStatement(sqlStatement);
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);
            }
            preparedStatement.execute();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

Implementation looks like this:

saveOrUpdate("INSERT INTO my_table(my_column) VALUES (?)", ConnectionUtil.getConnection(), "MyValue");

Nothing special. Except.. it doesn't work. No exceptions, nothing. But record is not inserted. Selection works as intended. It returns records. Then I swapped connections. I created connection in classic way:

  Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","u","p");

passed c instead of ConnectionUtil.getConnection() and it worked.

However, the logical question that follows this situation is:

If I cannot insert record by using connection returned by getConnection method, then how can Hibernate insert a record if it's using that same connection?

Upvotes: 0

Views: 67

Answers (1)

Predrag Maric
Predrag Maric

Reputation: 24433

The ConnectionProvider is not intended to be used by the application, check the documentation

The ConnectionProvider interface is not intended to be exposed to the application. Instead it is used internally by Hibernate to obtain connections.

So, the better option is your second approach, to get the connection in a classic way.

Upvotes: 1

Related Questions