Announcement Announcement Module
Collapse
No announcement yet.
spring transaction question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring transaction question

    New to spring. Could some one point me how to do multiple database operations within one transaction in spring 2.5? i.e. it functions simialr to:

    Connection conn.setAutoCommit(false);
    ...
    pstmt.executeUpdate(); // database operation 1
    pstmt.executeUpdate(); // database operation 2
    pstmt.executeQuery(); // database operation 3
    ...
    Connection conn.setAutoCommit(true);
    ...

    thanks

  • #2
    There are at least 2 possibilities:
    1. Each database is declared in its own separate XA-capable DataSource, and you must use a JTA transaction manager to synchronize updates.
    2. Find a way to gather all databases into a single java DataSource (eg. Oracle DB link or such), and use a simple transaction manager (no need to be jta).
    It is not possible to use raw JDBC with multiple datasources synchronized by a single transaction, unless you want to devlelop 2-phases commit by hand. If you opt for the first solution, then you should better use an ORM : JPA is the standard, and can join a JTA transaction.
    In this situation, you have to declare the following beans:
    • DataSource, typically a JNDI lookup
    • EntityManagerFactory, using the EMF factory bean provided by Spring; inject the DataSource into the EMF
    • TransactionManager, of type JtaTransactionManager or the implementation-specific subclass according to your application server

    If you can gather all databases into a single DataSource, the solution is simpler: you can use raw jdbc, but instead of doing the plumbing code by hand (get connection, create statement, execute it, close everything, handle exceptions, etc.), consider using Spring's JdbcTemplate.
    You must declare a transaction manager of type DataSourceTransactionManager, into which you inject the DataSource. The DAO are configured with that same DataSource. The DataSource does not need to be XA-capable. Spring will do the rest.

    Have a look at appropriate chapters in the Spring documentation related to the above-mentioned topics to get more information.

    Comment


    • #3
      thanks for reponse, but my question was

      How to ensure multiple database operations (same database, not distributed)
      will be executed within one transaction in Spring + hibernate?

      E.G.
      read information from table 1;
      according to what you get from step 1 above, load data from table 2;
      based on step 2, insert something to table 3;


      My question was: how to ensure all the above 3 operations within one transaction in spring? i.e. all the above 3 steps must be either all success or failure.

      thanks

      Comment


      • #4
        This is the reason why transactions exist : group multiple operations into a single unit of work, and either commit all or rollback all.

        When wrapping the operations into a (local) transaction, Spring stores the Hibernate session, or JDBC connection, or JPA EntityManager when the transaction starts and always reuses that same source for all queries issued inside the transaction. This (in addition with Hibernate or JPA support) ensures all database operations occurs in the same unit of work.

        Simply put, you have to declare a bean which type implements PlateformTransactionManager (there's one for raw JDBC: DataSourceTransactionManager, one for Hibernate: HibernateTransactionManager etc). You have to inject the source into the transaction manager (SessionFactory for Hibernate, DataSource for raw jdbc) and this is all for the configuration.

        In order to use it, you can use declarative or programmatic demarcation. Read the manual for further info.

        Comment


        • #5
          Example

          Code:
          @Transactional
          public void serviceMethod(Object args) {
               executeUpdate(); // database operation 1
               executeUpdate(); // database operation 2
               executeQuery(); // database operation 3
          }
          Config:
          HTML Code:
          <tx:annotation-driven transaction-manager="transactionManager"/>
          
          <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                  <property name="sessionFactory" ref="sessionFactory"/>
          </bean>
          
          <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
          	<property name="dataSource" ref="dataSource"/>
          	<property name="mappingLocations">
          	        <list>
          			<value>classpath:../YourMappingFile.hbm.xml</value>
          		</list>
          	</property>
          	<property name="hibernateProperties">
          		<props>
                  		<prop key="hibernate.format_sql">true</prop>
                  		<prop key="hibernate.show_sql">true</prop>
                		</props>
          	</property>
          </bean>
          Last edited by BramB75; Dec 10th, 2009, 03:19 AM.

          Comment

          Working...
          X