Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"TM" in Hibernate Code Causing Confusion

Tags:

java

hibernate

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);
        }
    }
}
like image 207
Eddy Freeman Avatar asked Jan 25 '16 06:01

Eddy Freeman


1 Answers

Exact Solution to your Question

  1. TM is definitely an Instance Variable, usually set for single database connection manager per test suite

  2. Please download the book source code from this link

  3. 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 :-)

  1. I would always recommend to get the source code online from manning publication sites for better practical understanding. They also upload their source code's in their official site which is relevant to the current context.
like image 57
Praveen Kumar K S Avatar answered Oct 14 '22 06:10

Praveen Kumar K S