Announcement Announcement Module
Collapse
No announcement yet.
HibernateTemplate and JdbcTemplate in one transaction ?? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HibernateTemplate and JdbcTemplate in one transaction ??

    Hi All,

    In my new application which I am designing, I am planning to use HibernateTemplate and JdbcTemplate at data access layer and planning to use declarative transactions using Spring DataSourceTransactionManager at service layer.

    The same datasource is used for building the JdbcTemplate and to build Hibernate Sessionfactory which is passed to HibernateTemplate . Do you see any issues with this approach ?. I am having single datasource and I don't have any distributed transactions.

    In one transaction I may use HibernateTemplate to access a table and in the same transaction I may use JdbcTemplate to access another table.

    Please let me know if you see any issues with this approach.

    Thanks in advance,
    Venkat

  • #2
    i do not test

    may be to do this transaction in the Action code :

    org.springframework.transaction.interceptor.Transa ctionProxyFactoryBean.getTransactionTemplate().exe cute(new TransactionCallback(){
    public Object doInTransaction(TransactionStatus ts){

    try {

    // your code
    //Connection conn = getConnection();
    //PreparedStatement stmt = conn.prepareStatement

    }catch (Exception e) {
    con.rollback();
    ts.setRollbackOnly();
    }

    return null;

    }
    })

    Comment


    • #3
      Originally posted by rvsridhar
      In one transaction I may use HibernateTemplate to access a table and in the same transaction I may use JdbcTemplate to access another table.

      Please let me know if you see any issues with this approach.
      I have not done this myself yet, but there should be no problem. You just have to ensure (as you said you did), that the DataSource you use for JdbcTemplate is the same as used in the SessionFactory.

      Regards,
      Andreas

      Comment


      • #4
        Venkat, mixing Jdbc with Hibernate inside the same transaction works - as the others said you just have to use the same datasource for both jdbcTemplate and the HibernateTemplate.
        Note that you might have some caching issues with Hibernate since a third party will be affecting the database on which Hibernate is working on.
        You can find more details on this topic through searching the forum.

        Comment


        • #5
          Originally posted by Costin Leau
          Note that you might have some caching issues with Hibernate since a third party will be affecting the database on which Hibernate is working on.
          Yes, I forgot to mention that. Though, if you use the JDBC code for reading only (which is the common case in this scenario) you should be fine.

          Regards,
          Andreas

          Comment


          • #6
            Afaik - DataSourceTransactionManager will not work with HibernateTemplate. On the other hand HibernateTransactionManager will work with both HibernateTemplate and JdbcTemplate.

            Comment


            • #7
              HibernateTemplate & JdbcTemplate in single transaction

              Originally posted by dejanp
              Afaik - DataSourceTransactionManager will not work with HibernateTemplate. On the other hand HibernateTransactionManager will work with both HibernateTemplate and JdbcTemplate.
              Is this true? I keep hearing that it is possible to mix JdbcTemplate and HibernateTemplate update operations in a single transaction, but I was always unsure how to define the transaction manager. If this is true, it will be nice to know.

              In addition, it makes perfect sense to me that if the JdbcTemplate operations are not merely reads, the Hibernate cache may be invalidated. However, in real situations, how should this issue be dealt with? I have read that calling HibernateTemplate.clear() will clear the cache, so this could be called after executing the JdbcTemplate operation. However, it also cancels all pending saves, updates, and deletes. Also, are there issues about when Hibernate flushes its state to the database in relation to JdbcTemplate's synchronous nature (i.e., does the Hibernate Session need to be flushed before the JdbcTemplate operation is executed)?

              How do people deal with using both JdbcTemplate and HibernateTemplate update operations in a single transaction?

              Thanks!

              -Arthur Loder

              Comment


              • #8
                Originally posted by Arthur Loder
                Is this true? I keep hearing that it is possible to mix JdbcTemplate and HibernateTemplate update operations in a single transaction, but I was always unsure how to define the transaction manager. If this is true, it will be nice to know.

                In addition, it makes perfect sense to me that if the JdbcTemplate operations are not merely reads, the Hibernate cache may be invalidated. However, in real situations, how should this issue be dealt with? I have read that calling HibernateTemplate.clear() will clear the cache, so this could be called after executing the JdbcTemplate operation. However, it also cancels all pending saves, updates, and deletes. Also, are there issues about when Hibernate flushes its state to the database in relation to JdbcTemplate's synchronous nature (i.e., does the Hibernate Session need to be flushed before the JdbcTemplate operation is executed)?

                How do people deal with using both JdbcTemplate and HibernateTemplate update operations in a single transaction?

                Thanks!

                -Arthur Loder

                From HibernateTransactionManager docs:

                This implementation is appropriate for applications that solely use Hibernate for transactional data access, but it also supports direct data source access within a transaction (i.e. plain JDBC code working with the same DataSource). This allows for mixing services that access Hibernate (including transactional caching) and services that use plain JDBC (without being aware of Hibernate)! Application code needs to stick to the same simple Connection lookup pattern as with DataSourceTransactionManager (i.e. DataSourceUtils.getConnection or going through a TransactionAwareDataSourceProxy).
                What I did usually was: flushing the hibernate session, do the jdbc stuff, reload your hibernate objects (you can use refresh for single instances or clear/load for all of them). The fact is that you usually know which instances can be touched by the jdbc update so you can optimize the process by refreshing only the ones where you know it make sense.

                Comment


                • #9
                  Originally posted by Arthur Loder
                  How do people deal with using both JdbcTemplate and HibernateTemplate update operations in a single transaction?
                  I can only say that for me usually the need arises to improve performance of selects (e.g. for reports). So I use Jdbc for reading only. Otherwise I always try to use the O/R mappers API for updates.

                  Besides that: for the current transaction the caching problem could be handled by properly ordering operations. If you do something like that, you should be aware of the implications. One other point is the second-level cache. If you update via Jdbc (or some other means, e.g. external processes) you should disable that cache.

                  Regards,
                  Andreas

                  Comment


                  • #10
                    Thanks a lot for all of you for sharing your thoughts.

                    The example that I am having seems to be working with both (Hibernate and Datasource) transaction managers. I have an insert into one table using JDBCTemplate and another insert into another table using Hibernate template and the transaction is controlled at service. My application is having major reads and few updates and inserts. I'll use JdbcTemplate only when I am dealing with large data sets. Based on my testing and your suggestion, I am planning to use HibernateTransactionManager. And it seems to be working. I don't have secondary cache enable.

                    Comment

                    Working...
                    X