Information Technology Reference
In-Depth Information
ds.setAllowLocalTransactions(true);
ds.setMaxPoolSize(3);
ds.getDriverProperties().put("driverClassName",
"org.h2.Driver");
ds.getDriverProperties().put("Url", "jdbc:h2:mem:mydb");
ds.getDriverProperties().put("password", "sasa");
ds.getDriverProperties().put("user", "sa");
ds.init();
}
The preceding code section will do two different things. First, which is easy to infer, is the
creation of the
PoolingDataSource
class (a data source class provided by
btm
).
What is hard to understand first-hand is that this is not our real data source, but just a
wrapper that we will use to provide transaction management around the
Lr-
cXADataSource
class (the actual in-memory database data source). Secondly, the pre-
ceding code section will also publish the created data source in the JNDI context. That
way, the persistence unit that we previously configured in the
persistence.xml
file
is going to be able to take the connection from the
jta-data-source
parameter con-
figuration:
<jta-data-source>jdbc/testDS</jta-data-source>
This way, the persistence unit will be using the specific data source information that we
define in our unit test. We just need to make sure that we create the data source wrapper
before instantiating the persistence unit's entity manager factory. This is something that
the code you create to start the persistence unit needs to do beforehand, and it is the main
reason we create the persistence unit's
EntityManagerFactory
object in the test
method, and start the data source wrapper in the
@Before
section:
@Test
public void testPersistentKieSessionInstantiation() throws
Exception {
KieServices ks = KieServices.Factory.get();
KieStoreServices kstore = ks.getStoreServices();
Environment environment = ks.newEnvironment();
EntityManagerFactory emf = Persistence.
createEntityManagerFactory("org.jbpm.persistence.jpa");
TransactionManager tm = TransactionManagerServices.