Skip to main content

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

Hi,
It's been a while since I've used AspectJ in anger but as no-one else has replied yet...

I'd say you probably want "args(manager)" instead of "target(manager)" in the obtainEntityManager pointcut.
The target of the call will be an AccountDAO, not an EntityManager.

See http://www.eclipse.org/aspectj/doc/released/progguide/semantics-joinPoints.html

HTH!
Mark.

--- On Wed, 4/11/09, neo anderson <javadeveloper999@xxxxxxxxxxx> wrote:

> From: neo anderson <javadeveloper999@xxxxxxxxxxx>
> Subject: [aspectj-users] A transaction question
> To: aspectj-users@xxxxxxxxxxx
> Date: Wednesday, 4 November, 2009, 12:09
> 
> 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