Question

For our web application, all DB access is performed via stored procedures. This design decision is set in stone and there is no way I can change it.

Additionally, all transaction handling is done within the stored procedures. The Java layer must not use transactions.

I can easily accomplish this using plain JDBC. But I’d prefer to use Hibernate for its ORM and caching features.

Calling stored procedures with Hibernate is easy:

final Session session = sessionFactory.getCurrentSession();
final Query query = session.createSQLQuery("EXEC dbo.SP_ProductSearch :searchStr").addEntity(Product.class).setString("searchStr", "ap");
return query.list();

But when I run a DB profile, I see the following.

SET IMPLICIT_TRANSACTIONS ON
EXEC dbo.SP_ProductSearch  N'ap'
IF @@TRANCOUNT > 0 COMMIT TRAN
IF @@TRANCOUNT > 0 COMMIT TRAN
SET IMPLICIT_TRANSACTIONS OFF

Is there anyway I can disable the 4x transaction statements?

My technology stack is as follows.

  • Spring 3.1.2
  • Hibernate 4.1.8
  • jTDS (JDBC driver) 1.2.5
  • MS SQL Server 10.x

Many thanks in advance... Adam.


@ctapobep - this is how it is done using plain JDBC.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class PlainJdbcTest
{
    private static final String USERNAME = "sa";
    private static final String PASSWORD = "password";
    private static final String URL = "jdbc:jtds:sqlserver://myserver:1433;DatabaseName=testDatabase;prepareSQL=0";
    private static final String QUERY = " EXEC dbo.SP_ProductSearch ?";
    private static final String DRIVER = "net.sourceforge.jtds.jdbc.Driver";


    public static void main(String[] args) throws Exception
    {
        Class.forName(DRIVER).newInstance();

        final Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        final PreparedStatement statement = connection.prepareStatement(QUERY);
        statement.setString(1, "ba");
        final ResultSet resultSet = statement.executeQuery();

        while(resultSet.next())
        {
            final int id = resultSet.getInt(1);
            final String desc = resultSet.getString(2);
            System.out.print(id + " / " + desc);
        }

        if(resultSet != null) resultSet.close();
        if(statement != null) statement.close();
        if(connection != null) connection.close();
    }
}

And this is what is shown in the profile - so works perfectly!

EXEC dbo.SP_ProductSearch  N'ba'
Was it helpful?

Solution 2

I haven't discovered any way to prevent Hibernate from using transactions, so as a work-around I've switched to MyBatis which works perfectly.

http://www.mybatis.org/spring/index.html

OTHER TIPS

How are you calling your hibernate code (executing stored procedure)? Is it called through Spring Service class having @Transactional annotation?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top