title: How to get connection from EntityManager configured in Spring context?
body: <p>I want to get connection from EntityManager configured in spring context to use it in dbunit DatabaseOperation.CLEAN_INSERT.execute method.</p>
  
  <p>Here is Spring configuration:</p>
  
  <pre><code>&lt;bean id="testmEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;qualifier value="testm" type="org.springframework.beans.factory.annotation.Qualifier"/&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="jpaDialect"&gt;&lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/&gt;&lt;/property&gt;
      &lt;property name="jpaProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/prop&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.H2Dialect&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
     &lt;property name="jpaVendorAdapter" ref="jpaAdapter"/&gt;
      &lt;property name="loadTimeWeaver"&gt;
          &lt;bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
   &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource"
        p:driverClassName="org.h2.Driver"
        p:url="jdbc:h2:mem:test;IGNORECASE=TRUE;"&gt;
      &lt;property name="autoCommit" value="false"/&gt;
      &lt;property name="suppressClose" value="true"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;&lt;/bean&gt;
  
   &lt;tx:annotation-driven transaction-manager="testmTransactionManager"/&gt;
  &lt;bean id="testmTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;qualifier value="testm"/&gt;
      &lt;property name="entityManagerFactory" ref="testmEntityManagerFactory"/&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Here is code:</p>
  
  <pre><code>@javax.persistence.PersistenceContext(unitName = "testm")
  @Qualifier(value = "testm")
  protected javax.persistence.EntityManager emanager;
  
  
  @Transactional
  public void setUp() throws Exception {
      IDatabaseConnection connection = new DatabaseConnection (((SessionImpl)(emanager.getDelegate())).connection());
  </code></pre>
  
  <p>I receive exception <code>org.hibernate.SessionException: Session is closed!</code></p>
  
  <p>When I use <code>emanager.getTransaction ().begin ();</code>
  I get <code>java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead</code></p>
  
  <p><strong>SOLVED:</strong>
  used datasource to get it:</p>
  
  <pre><code>@Autowired
  private DataSource dataSource;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>dataSource.getConnection ()
  </code></pre>