Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String args[])
- {
- Weld weld = new Weld();
- WeldContainer container = weld.initialize();
- ShopCar sc = container.instance().select(ShopCar.class).get();
- sc.execute();
- weld.shutdown();
- }
- /**
- *
- * @author vFreitas
- * @param <T> The type T
- */
- public class JpaDAO<T> implements DAO<T>, Serializable
- {
- /* The EntityManager of my connection */
- private final EntityManager em;
- /* The class to be persist */
- private final Class<T> classe;
- private ThreadLocal<EntityManager> threadLocal;
- /* Builder */
- /**
- *
- * @author info1
- * @param classe The class to that will represent T
- * @param em A new instance of EntityManager
- */
- public JpaDAO(Class<T> classe, EntityManager em)
- {
- this.classe = classe;
- this.em = em;
- threadLocal = new ThreadLocal<>();
- threadLocal.set(em);
- }
- @Override
- @Transacional
- public void save(T entity)
- {
- //em.getTransaction().begin();
- em.persist(entity);
- //em.getTransaction().commit();
- }
- ...
- public class DAOFactory<T>
- {
- @Inject @MyDatabase private EntityManager em;
- @SuppressWarnings({ "rawtypes", "unchecked" })
- @Produces
- public JpaDAO<T> createJpaDAO(InjectionPoint injectionPoint) throws
- ClassNotFoundException
- {
- ParameterizedType type = (ParameterizedType) injectionPoint.getType();
- Class classe = (Class) type.getActualTypeArguments()[0];
- return new JpaDAO<>(classe,em);
- }
- }
- @Target({ ElementType.METHOD, ElementType.TYPE })
- @Retention(RetentionPolicy.RUNTIME)
- @InterceptorBinding
- public @interface Transacional
- {
- }
- @Interceptor
- @Transacional
- public class TransacionalInterceptor
- {
- @Inject @MyDatabase
- private EntityManager manager;
- @Inject private ThreadLocal<EntityManager> threadLocal;
- Logger logger = LoggerFactory.getLogger(TransacionalInterceptor.class);
- @AroundInvoke
- public Object invoke(InvocationContext context) throws Exception
- {
- manager = threadLocal.get();
- //EntityTransaction trx = manager.getTransaction();
- if(!manager.getTransaction().isActive())
- {
- manager.getTransaction().begin();
- System.out.println("Starting transaction");
- Object result = context.proceed();
- manager.getTransaction().commit();
- System.out.println("Committing transaction");
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement