title: Groovy / Hibernate / @Transaction returns No Session found for current thread
body: <p>I have a Spring/JPA/Groovy/Hibernate stack, (note: <strong>not grails</strong>), and I'm finding that I'm getting a <code>No session found for current thread</code> when trying to perform some <code>@Transactional</code> work on startup.</p>
  
  <p>I have a controller class that, upon <code>@PostConstruct</code>, invokes a <code>@Transactional</code> method on another class which tries to populate the database with some sample data to be used in a simulation.</p>
  
  <p>Here's the <code>controller</code> class:</p>
  
  <pre><code>@Component
  public class SimulationController {
  
      private TransactionTemplate transactionTemplate;
      @Autowired
      private PlatformTransactionManager transactionManager;
      @Autowired
      private IPublisher publisher;
  
      @PostConstruct
      public void intialize()
      {
          this.transactionTemplate = new TransactionTemplate(transactionManager);
          transactionTemplate.execute(new TransactionCallbackWithoutResult() {
              @Override
              protected void doInTransactionWithoutResult(TransactionStatus arg0) {
                  racePublisher.populateData();
              }
          });
      }
      //  Also tried, with no success:    
      //  @PostConstruct
      //  public void initialize()
      //  {
      //      publisher.populateData();
      //  }
  }
  </code></pre>
  
  <p>As you can see, I've moved away from a pure <code>@Transactional</code> approach in the <code>@PostConstruct</code> for reasons discussed <a href="http://forum.springsource.org/showthread.php?58337-No-transaction-in-transactional-service-called-from-PostConstruct" rel="nofollow">here</a>.</p>
  
  <p>My <code>IPublisher</code> is a groovy class, as follows:</p>
  
  <pre><code>@Component
  class Publisher implements IPublisher {
  
  @Autowired
  IStockDAO stockDAO
  
  void populateData()
  {
      createStock()
  }
  @Transactional
  void createStock()
  {
      def list = [new Stock(ticker: "ADBE", name: "Adobe"),
                  new Stock(ticker: "MSFT", venueCode: "Microsoft")]
      list.each { stockDAO.create it }
  }
  </code></pre>
  
  <p>Which is an implementation of:</p>
  
  <pre><code>public interface IPublisher {
  
      public void populateData();
      public void createStock();
  }
  </code></pre>
  
  <p>Note, I've also tried marking <code>populateData()</code> as <code>@Transactional</code>, with no effect.</p>
  
  <p>In my Spring context class, I'm defining <code>&lt;tx:annotation-driven/&gt;</code>.</p>
  
  <p>As far as I can tell, I've done everything correctly.  However, I can't seem to get this to work. </p>
  
  <p>What else is required?</p>
  
  <p><strong>Updated:</strong>
  Here are the beans setting up my dataAccess related beans:</p>
  
  <pre><code>&lt;beans&gt;
  &lt;bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven/&gt;
  
      &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://${database.host}:${database.port}/${database.name}" /&gt;
          &lt;property name="username" value="${database.username}" /&gt;
          &lt;property name="password" value="${database.password}" /&gt;
          &lt;property name="initialSize" value="5" /&gt;
          &lt;property name="maxActive" value="50" /&gt;
      &lt;/bean&gt;
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="packagesToScan"&gt;
              &lt;list&gt;
                  &lt;value&gt;com.mangofactory.concorde&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;
                  &lt;prop key="hibernate.cache.provider_class"&gt;org.hibernate.cache.HashtableCacheProvider
                  &lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;&lt;!--  use create for full drop/create --&gt;
                  &lt;prop key="hibernate.connection.autocommit"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.statement_cache.size"&gt;0&lt;/prop&gt;
                  &lt;prop key="hibernate.jdbc.batch_size"&gt;20&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;beans&gt;
  </code></pre>