I am studying this book Java Persistence with Hibernate
and on page 24 in the PDF version there is a statement like :
UserTransaction tx = TM.getUserTransaction();
I don't know where the TM
comes from. I have googled for a while but can't find any answer. Because of this i can't run my code in Netbeans.
I can also see JPA
somewhere in the code. What does that also mean?
Thanks for helping
The whole code is :
public class HelloWorldJPA {
public static void main(String[] args){
try {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("HelloWorldPU");
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
} catch (NotSupportedException | SystemException ex) {
Logger.getLogger(HelloWorldJPA.class.getName()).log(Level.SEVERE, null, ex);
}
EntityManager em = emf.createEntityManager();
Message message = new Message();
message.setText("Hello World!");
em.persist(message);
tx.commit();
em.close();
} catch (HeuristicMixedException | HeuristicRollbackException | IllegalStateException | RollbackException | SecurityException | SystemException ex) {
Logger.getLogger(HelloWorldJPA.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Exact Solution to your Question
TM is definitely an Instance Variable, usually set for single database connection manager per test suite
Please download the book source code from this link
Look for TransactionManagerTest.java class
public class TransactionManagerTest {
// Static single database connection manager per test suite
static public TransactionManagerSetup TM;
@Parameters({"database", "connectionURL"})
@BeforeSuite()
public void beforeSuite(@Optional String database,
@Optional String connectionURL) throws Exception {
TM = new TransactionManagerSetup(
database != null
? DatabaseProduct.valueOf(database.toUpperCase(Locale.US))
: DatabaseProduct.H2,
connectionURL
);
}
@AfterSuite(alwaysRun = true)
public void afterSuite() throws Exception {
if (TM != null)
TM.stop();
}
}
You will get the answer :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With