Announcement Announcement Module
Collapse
No announcement yet.
Best practice for direct SQL with hiberate/spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Best practice for direct SQL with hiberate/spring

    Hi,

    we are using Hibernate together with Spring 1.1. Our Beans are configured with the TransactionProxyFactoryBean.
    For performance reasons we have to use direct JDBC and we are not sure that we use it correct or in the best way.

    The Code:
    ...
    Statement statement = null;
    List list = new ArrayList();
    try
    {
    Connection connection = getSession().connection();
    statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery(sql);
    while (resultSet.next())
    {
    AnObject anObject = new AnObject(resultSet.getString(1), resultSet
    .getString(2));
    list.add(anObject);
    }
    } catch (HibernateException e)
    {
    throw SessionFactoryUtils.convertHibernateAccessExceptio n(e);
    } finally
    {
    if (statement != null)
    statement.close();
    }
    ...

    The connection is closed by the Spring container - correct ? Is there a better way to do direct SQL from within a "TransactionProxyFactoryBean"-bean ?

  • #2
    You can implement your JDBC code using Spring JDBC abstraction layer by subclassing JdbcDaoSupport. Then use the same transaction manager for both hibernate and jdbc daos. To access the jdbc connection used by Hibernate from within JDBC dao you can use:
    Code:
    Connection con = DataSourceUtils.getConnection(dataSource);
    HTH

    Comment

    Working...
    X