I was trying to write a DAO class and use the EntityManager inside this class to perform persistance operations. My database is defined as a jta-data-source in my persistence.xml and registered accordingly as a datasource in my web application server.
So I run into a problem when using the em.persist() method of the EntityManager inside my DAO because I need a transaction. Apparently, it must be a special type of transaction called UserTransaction.
Searching for examples in the web I find that you can inject this transaction with @Resource UserTransaction utx;
But for the injection to work, my DAO class has to be initialized by the container by making it an EJB (@Stateless). Making my DAO container-managed prevents me to freely and manually initialize my DAO class.
How can I create transactions inside my DAO class without making it an EJB? Do I have to manually look in the JNDI for the UserTransaction?