Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] A transaction question

Just a quick observation:

 protected pointcut obtainEntityManager(EntityManager manager): call(*
example.AccountDao.setEntityManager(EntityManager)) && target(manager);

shouldn't that be:

 protected pointcut obtainEntityManager(EntityManager manager): call(*
example.AccountDao.setEntityManager(EntityManager)) && args(manager);

As the EntityManager is the argument to the method call and not the
target of the call (the target of the call being AccountDao)?

Andy

2009/11/4 neo anderson <javadeveloper999@xxxxxxxxxxx>:
>
> I am learning how to modulize transaction using aspectj, but encounter a
> problem that EntityManager I try to capture is always null. The compiler
> issues message saying that the advice can not be applied. as below:
>
> ... advice defined in example.AbstractTransaction has not been applied
> [Xlint:adviceDidNotMatch]
>
> What should I change so that I can capture the EntityManager while it is
> created (whilst calling to AccountDao.setEntityManager(..))?
>
> Thanks for help.
>
> Main.java
>
> package example;
>
> public class Main{
>        public static void main(String args[]){
>                Main m = new Main();
>                m.process();
>        }
>        void process(){
>                AccountDao dao = new AccountDao();
>
> dao.setEntityManager(PersistenceCreator.createEntityManagerFactory().createEntityManager());
>                User u = new User("1", "Smith");
>                u.setAddress("123 Test Road, London.");
>                dao.save(u);
>
>                dao.list();
>        }
> }
>
> AccountDao.java
>
> package example;
>
> import java.util.List;
> import java.util.ArrayList;
>
>
> public class AccountDao{
>
>        private EntityManager manager;
>
>        private static List<User> database = new ArrayList<User>();
>
>        public void setEntityManager(EntityManager manager){
>                this.manager = manager;
>        }
>
>        public void save(User user){
>                database.add(user);
>        }
>
>        public void list(){
>                for(User u : database){
>                        System.out.println(">>>[AccountDao.java]"+u);
>                }
>        }
> }
>
> User.java
>
> package example;
>
> public class User{
>        private String id;
>        private String name;
>        private String address;
>        public User(String id, String name){
>                this.id = id;
>                this.name = name;
>        }
>
>        public String getId(){
>                return this.id;
>        }
>
>        public String getName(){
>                return this.name;
>        }
>
>        public String getAddress(){
>                return this.address;
>        }
>
>        public void setAddress(String address){
>                this.address = address;
>        }
>
>        public String toString(){
>                return "<User [id:"+id+"][name:"+name+"][address:"+address+"]>";
>        }
> }
>
>
> PersistenceCreator.java
>
> package example;
>
> public class PersistenceCreator{
>        private static EntityManagerFactory factory;
>
>        public static EntityManagerFactory createEntityManagerFactory(){
>                if(null == factory)
>                        factory = new EntityManagerFactory();
>                return factory;
>        }
> }
>
> EntityManagerFactory.java
>
> package example;
>
> public class EntityManagerFactory{
>
>        private static EntityManager manager;
>
>        public static EntityManager createEntityManager(){
>                if(null == manager){
>                        manager = new EntityManager();
>                }
>                return manager;
>        }
> }
>
> EntityManager.java
>
> package example;
>
> public class EntityManager{
> }
>
> AbstractTransaction.aj
>
> package example;
>
> public abstract aspect AbstractTransaction percflow(scope()){
>
>        private EntityManager manager;
>
>        protected abstract pointcut tx();
>
>        protected pointcut obtainEntityManager(EntityManager manager): call(*
> example.AccountDao.setEntityManager(EntityManager)) && target(manager);
>
>        protected pointcut scope(): tx() && !cflowbelow(tx());
>
>        Object around(): scope(){
>                System.out.println("EntityManager:"+manager);// always null
>                Object result = proceed();
>                return result;
>        }
>
>        EntityManager around(EntityManager manager): obtainEntityManager(manager)
> && cflow(tx()){
>                if(null == manager){
>                        manager = proceed(manager);
>
>                }
>                return manager;
>        }
> }
>
> AccountTransaction.aj
>
> package example;
>
> public aspect AccountTransaction extends AbstractTransaction{
>
>        protected pointcut tx(): execution(* example.Main.process());
> }
> --
> View this message in context: http://old.nabble.com/A-transaction-question-tp26195112p26195112.html
> Sent from the AspectJ - users mailing list archive at Nabble.com.
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>


Back to the top