title: How do I get the connection inside of a Spring transaction?
body: <p>Imagine this code:</p>
  
  <pre><code>foo() {
       Connection conn = ...;
  }
  </code></pre>
  
  <p><code>foo()</code> has been called from a method that has the annotation <code>@Transactional</code>. How do I get the current JDBC connection? Note that <code>foo()</code> is in a bean (so it can have <code>@Autowired</code> fields) but <code>foo()</code> can't have parameters (so I can't pass the connection in from somewhere).</p>
  
  <p><strong>[EDIT]</strong> I'm using jOOQ which needs either a data source or a connection. My problem: I don't know which transaction manager is configured. It could be anything; Java EE, DataSource based, something which gets the data source via JNDI. My code isn't an application, it's a library. I need to swallow what others put on my plate. Along the same lines, I can't request a Hibernate session factory because the application using me might not use Hibernate.</p>
  
  <p>But I know that other code, like the Spring Hibernate integration, somehow can get the current connection from the transaction manager. I mean, Hibernate doesn't support Spring's transaction manager, so the glue code must adapt the Spring API to what Hibernate expects. I need to do the same thing but I couldn't figure out how it works.</p>
  
  <p><strong>[EDIT2]</strong> I know that there is an active transaction (i.e. Spring has a Connection instance somewhere or at least a transaction manager which can create one) but my method isn't @Transactional. I need to call a constructor which takes <code>java.sql.Connection</code> as  parameter. What should I do?</p>