title: spring jdbc + PlatformTransactionManager + spring data + theory
body: <p>I did start learn tool kit <code>spring jdbc</code>. Read some documentation about <a href="http://static.springsource.org/spring/docs/3.2.1.RELEASE/spring-framework-reference/html/transaction.html#transaction-programmatic" rel="nofollow">transactions</a> 
  in spring end their templates. Nevertheless some common things not clear to me. </p>
  
  <p>1) <strong>If we have <code>Spring Data</code> why always heard only about <code>Spring JDBC</code></strong><br/>
  Spring framework have some project like <code>Spring MVC</code>, <code>Spring Security</code> and etc.<br/> First i try to find <code>Spring JDBC</code> on home site of Spring, but do not found it. Instead of i found <code>Spring Data</code> project. After some research i found what <code>Spring Data</code> used <code>Spring JDBC</code> in <code>JDBC Extensions</code> sub-project and last have some <code>oracle</code> specific operation what interesting to me. And i realize what did not see or hear any use or see references in tutorials to <code>Spring Data</code>. It really something bad?
  <br/><br/></p>
  
  <p>2) <strong>Should I create new instance of JdbcTemplate every time</strong><br/>
  Next was <code>JdbcTemplate</code>, useful template method. All docs replete with examples of code like</p>
  
  <pre><code>public class JdbcCorporateEventDao implements CorporateEventDao {
  
      private JdbcTemplate jdbcTemplate;
  
      public void setDataSource(DataSource dataSource) {
          this.jdbcTemplate = new JdbcTemplate(dataSource);
      }
  
      // JDBC-backed implementations of the methods on the CorporateEventDao follow...
  }
  </code></pre>
  
  <p>And they also write in docs</p>
  
  <blockquote>
    <p> Instances of the JdbcTemplate class are threadsafe once configured</p>
  </blockquote>
  
  <p>This is misleading. Why in <code>setDataSource</code> method they create new instance if they can put <code>dataSource</code> in already created one or i misunderstood it?
  <br/><br/></p>
  
  <p>3) <strong>How can we use <code>TransactionTemplate</code> for writing sophisticated client logic?</strong><br/>
  <code>TransactionTemplate</code> another one template what works with <code>TransactionStatus</code>. As I understand it can help me manage my transaction <em>but</em> how much help?
  At the start of <code>execute</code> method of <code>TransactionTemplate</code> we have <code>transactionManager.getTransaction(this)</code>. At the end of <code>execute</code> method  we have line <code>this.transactionManager.commit(status)</code>. Therefore, how i understand it, all what i place in <code>doInTransaction</code> method will execute in single transaction. But how about execute other <code>DAO</code>s with same transaction in another methods? It constrains to be writing client with sophisticated logic. I mean all logic must be in one method? I thought it can not be true.<br/>
  What i mean when say sophisticated logic. For example i have my own template method.</p>
  
  <pre><code>/*abstract class definition*/
  
  public final void execute(){
      onPreExec();
      exec();
      onPostExec();
  }
  
  abstract void exec(); //client execute few DAOs methods
  
  public void onPreExec(){}
  
  public void onPostExec(){} //commit or rollback transaction in another method
  
  /*other class members*/
  </code></pre>
  
  <p><br/><br/></p>
  
  <p>4) <strong>Is thread-safe to use "PTM", "TD", "TS"?</strong><br/>
  Further i begin investigate what stand behind <code>this.transactionManager.commit(status)</code>. This represent to me <code>PlatformTransactionManager</code> and <code>TransactionDefinition</code>. How i understand, <del>at the moment when i start write this line of text</del>, this classes can help me achieve my goal in question #3. For example i can do like this:</p>
  
  <pre><code>/*abstract class definition*/
  
  protected PlatformTransactionManager ptm;
  protected TransactionDefinition td;
  protected TransactionStatus ts;//TS with PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED
  
  public final void execute(){
      onPreExec();
      exec();
      onPostExec();
  }
  
  abstract void exec(); //client execute few DAOs methods
  
  public void onPreExec(){//start transaction
      ts=ptm.getTransaction(td);
  }
  
  public void onPostExec(){//end transaction
      if (exec.wasCompletedSuccessfully()){
         dao.markJobCompleted(); //do some for fix execution completeness
         ptm.commit(ts);
      } else {ptm.rollback(ts);}
  
  }
  
  /*other class members*/
  </code></pre>
  
  <p>At least this looks more convenient than <code>transactionTemplate.execute()</code> method for some cases. Although merely divided into several parts <code>transactionTemplate.execute()</code> method.</p>
  
  <p>But still not clear it is thread-safe? i.e. can i use it and be sure that all inner call of <code>JDBC</code>s <code>callableStatment.execute()</code> methods from <code>jdbcTemplate</code> will refers to this and only to this transaction. And not see another transaction in other threads.
  <br/><br/></p>
  
  <p>Thanks for reading.</p>