title: Spring boot test - EntityManager does not persist when using 2 databases
body: <p>I have Spring Boot test for controller:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest
  @AutoConfigureMockMvc
  public class ExampleTest {
  
      @Autowired
      private MockMvc mockMvc;
  
      @Autowired
      private ExampleDAO;
  
      @Test
      public void someTest(){
  
          exampleDAO.save(someEntity);
  
          // call to endpoint
      }
  } 
  </code></pre>
  
  <p>I have two database configs:</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(entityManagerFactoryRef = "primaryManagerFactory", basePackages = { "com.example.repository.primary" })
  public class PrimaryDbConfig {
  </code></pre>
  
  <p>And</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(entityManagerFactoryRef = "anotherManagerFactory", basePackages = { "com.example.repository.another" })
  public class AnotherDbConfig {
  </code></pre>
  
  <p>Test properties fragment:</p>
  
  <pre><code>spring.primary.datasource.url=jdbc:h2:mem:primary;DB_CLOSE_ON_EXIT=FALSE
  spring.primary.datasource.username=sa
  spring.primary.datasource.password=
  spring.primary.datasource.driverClassName=org.h2.Driver
  
  spring.another.datasource.url=jdbc:h2:mem:another;DB_CLOSE_ON_EXIT=FALSE
  spring.another.datasource.username=sa
  spring.another.datasource.password=
  spring.another.datasource.driverClassName=org.h2.Driver
  
  spring.jpa.properties.hibernate.show_sql=true
  </code></pre>
  
  <p>GenericDAOImpl fragment:</p>
  
  <pre><code>@PersistenceContext(unitName = "another-persistence-unit")
  protected EntityManager entityManager;
  
  @Override
  @Transactional
  public void save(T entity) {
      entityManager.persist(entity);
  }
  </code></pre>
  
  <p>When I run test, <code>exampleDAO.save(someEntity)</code> method does not save or output any SQL to logs. <code>exampleDAO</code> is not primary data source. </p>
  
  <p>If I change to:</p>
  
  <pre><code>@Override
  @Transactional
  public void save(T entity) {
      entityManager.unwrap(Session.class).save(entity);
  }
  </code></pre>
  
  <p>It works well and persists data. However, the same method with primary data source DAO persists entities with <code>EntityManager</code>. </p>
  
  <p>Why does it save entity with <code>Session</code> but not with <code>EntityManager</code>?</p>