Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] AspectJ and Dao class

You may want to look at https://springmodules.dev.java.net/ which include db4o integration with Spring.

-Ramnivas

On 8/23/07, fyu33 <fyu33@xxxxxxxxxxx> wrote:

Ramnivas,

Thank you for your answer.
Actually for a good reason we dont want to use or reply on spring/hibernate:
we are using db4o as persistence layer. To my original question, I wrote an
aspect as the following:

public aspect ConnectionControl {
public pointcut connectionControl(XxxDao callee):execution(public *
XxxDao.*(..) )&&target(callee);
Object around(XxxDao callee) throws DaoException:connectionControl(callee){
        Object o = null;
        try {
                callee.setConnection(callee.getDataSource().getConnection());
                o = proceed(callee);
        } catch (ResourceException e) {
                 e.printStackTrace();
                throw new DaoException(e);
        }
        return o;
}
}

and XxxDao is like this:

public class XxxDao {
        private DataSource ds;
        private Connection cx;
...
        public Xxx get(int id){
           ...
          simply using cx set by the aspect ConnectionControl
          ...
        }

       public DataSource getDataSource(){
          return ds;
      }

      public void setConnection(Connection cx){
         this.cx = cx;
     }
...
}

As I am a newbie to AOP, I am wondering whether there are better ways to do
that, more precisely to access instance variables of XxxDao.

By the way, as I said, we use db4o, so the code shown here has been adapted.

Thank you again for your help!

Franck

Ramnivas Laddad wrote:
>
> While I can think of ways for AspectJ to help here, a more appropriate
> solution might be to use Spring's JDBCTemplate class. It solves this
> problem
> and much more. Note that you can use this class even if your architecture
> is
> not based on Spring i.e. you can use Spring just like any other library.
>
> -Ramnivas
>
> On 8/22/07, fyu33 <fyu33@xxxxxxxxxxx> wrote:
>>
>> Hello, In a JDBC DAO implementatin class, we usually see the following
>> pattern: ... private DataSource ds; public aMethod(){ Connection cx =
>> null;
>> try{ cx = ds.getConnection (); ... }catch(Exception e){ ... }finally{
>> if(cx!=null){ try{ cx.close(); }catch(){ } } ... } it is tiring to code
>> the same thing in all DAO methods. I am looking for a sample code showing
>> how to use AspectJ to handle this. Thanks! Franck
>> ------------------------------
>> View this message in context: AspectJ and Dao
>> class<http://www.nabble.com/AspectJ-and-Dao-class-tf4311292.html#a12273585 >
>> Sent from the AspectJ - users mailing list
>> archive<http://www.nabble.com/AspectJ---users-f2219.html>at
>> Nabble.com.
>>
>> _______________________________________________
>> aspectj-users mailing list
>> aspectj-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>>
>>
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
>

--
View this message in context: http://www.nabble.com/AspectJ-and-Dao-class-tf4311292.html#a12288761
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