Mapper class and session

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Mapper class and session

rhummy
Hi all !

I would like to know if there is some way to reuse instanciated
mapperclass from another session ?
I have to insert into a table a bunch of data and I do it inside a for
loop.

SqlSession session = sqlMapper.openSession();
try{
PosterMapper poster = session.getMapper(PosterMapper.class);
poster.insert(aposter);
session.commit();

for (....){
try{
session = sqlMapper.openSession();
PostMapper p = session.getMapper(PostMapper.class);
p.insert(....)
session.commit();
}catch(Exception e){
session.rollback();
}finally{
session.close();
}
}


}catch(Exception e){ // This catch is for the first try (the poster's
insert)
session.rollback();
}finally{
session.close();
}
}

If p (the instanciated PostMapper) was instanciated before the first
commit in order to avoid the multiple getMapper inside the for loop,
it does not work.
It only works if I do like I wrote above.
Is there a way to instanciate all the mapper I will need and then use
them inside every session I will open ?
The "for" loop is because I want all the insert which do not violate
integrity to be saved and other discarded. I need to know witch went
wrong and which went good since there if it went good, I add the
comments of the current post too in the database.

Thank you for your answers.
Reply | Threaded
Open this post in threaded view
|

Re: Mapper class and session

Hunter
You shouldn't need to close the session after each rollback / commit.
You also only need one Mapper for each SqlSession you open.
Does something like this work?

SqlSession session = sqlMapper.openSession()

PosterMapper poster = session.getMapper(PosterMapper.class);
PostMapper p = session.getMapper(PostMapper.class);

try {
  poster.insert(aposter);

  for (....){
    try{
      p.insert(....)
      session.commit();
    } catch(Exception e){  // catch each p.insert that fails
      session.rollback();
    }
  }
} catch(Exception e){ // catch the failed poster.insert
  session.rollback();
}  finally {
  session.close();
}

On Oct 19, 9:12 pm, rhummy <[hidden email]> wrote:

> Hi all !
>
> I would like to know if there is some way to reuse instanciated
> mapperclass from another session ?
> I have to insert into a table a bunch of data and I do it inside a for
> loop.
>
> SqlSession session = sqlMapper.openSession();
> try{
> PosterMapper poster = session.getMapper(PosterMapper.class);
> poster.insert(aposter);
> session.commit();
>
> for (....){
> try{
> session = sqlMapper.openSession();
> PostMapper p = session.getMapper(PostMapper.class);
> p.insert(....)
> session.commit();}catch(Exception e){
>
> session.rollback();
>
> }finally{
> session.close();
> }
> }
> }catch(Exception e){ // This catch is for the first try (the poster's
>
> insert)
> session.rollback();
>
> }finally{
> session.close();
> }
> }
>
> If p (the instanciated PostMapper) was instanciated before the first
> commit in order to avoid the multiple getMapper inside the for loop,
> it does not work.
> It only works if I do like I wrote above.
> Is there a way to instanciate all the mapper I will need and then use
> them inside every session I will open ?
> The "for" loop is because I want all the insert which do not violate
> integrity to be saved and other discarded. I need to know witch went
> wrong and which went good since there if it went good, I add the
> comments of the current post too in the database.
>
> Thank you for your answers.
Reply | Threaded
Open this post in threaded view
|

Re: Mapper class and session

rhummy
Hello,

With your code, if the first post inserted by p.insert(...) has an
exception, all the next posts will be discarded since the poster
insertion is cancelled by the session.rollback() inside the for loop.
For example, if we have 3 posts and the second post raise an
exception, the third will not be inserted in the database. But the
poster and the first post will be inserted.
If a post inserted by p.insert(...) has an exception, I want it
discarded and I want continue with all the remaining posts.
At the end, the database will be populated with all the posts which
have not raised an exception.

Thank you for your help Hunter ! I understand now that the
session.commit() does not work like in SQL. In SQL, if I commit
something, it decreases the BEGIN/COMMIT counter. And if the counter
is equal to 0, I have to begin another BEGIN in order to start a
transaction.
Here, in myBatis, it is like sqlMapper.openSession() does a BEGIN,
session.commit does a COMMIT and a BEGIN, and session.close() does a
COMMIT. Correct me if I am wrong.

To avoid the "first post issue", I added a "commit" after the
poster.insert(aposter). Correct me if I have done something wrong :

try {
  poster.insert(aposter);
  session.commit(); // commit the poster in order to let subsequent
post (after the first exception) to be committed (it is because the
id_poster will not be available to them and an exception will be
raised even if the post is ok)


  for (....){
    try{
      p.insert(....)
      session.commit();
    } catch(Exception e){  // catch each p.insert that fails
      session.rollback();
    }
  }
} catch(Exception e){ // catch the failed poster.insert

  session.rollback();

}  finally {
  session.close();
}




On Oct 19, 10:38 pm, Hunter <[hidden email]> wrote:

> You shouldn't need to close the session after each rollback / commit.
> You also only need one Mapper for each SqlSession you open.
> Does something like this work?
>
> SqlSession session = sqlMapper.openSession()
>
> PosterMapper poster = session.getMapper(PosterMapper.class);
> PostMapper p = session.getMapper(PostMapper.class);
>
> try {
>   poster.insert(aposter);
>
>   for (....){
>     try{
>       p.insert(....)
>       session.commit();
>     } catch(Exception e){  // catch each p.insert that fails
>       session.rollback();
>     }
>   }} catch(Exception e){ // catch the failed poster.insert
>
>   session.rollback();
>
> }  finally {
>   session.close();
> }
>
> On Oct 19, 9:12 pm, rhummy <[hidden email]> wrote:
>
> > Hi all !
>
> > I would like to know if there is some way to reuse instanciated
> > mapperclass from another session ?
> > I have to insert into a table a bunch of data and I do it inside a for
> > loop.
>
> > SqlSession session = sqlMapper.openSession();
> > try{
> > PosterMapper poster = session.getMapper(PosterMapper.class);
> > poster.insert(aposter);
> > session.commit();
>
> > for (....){
> > try{
> > session = sqlMapper.openSession();
> > PostMapper p = session.getMapper(PostMapper.class);
> > p.insert(....)
> > session.commit();}catch(Exception e){
>
> > session.rollback();
>
> > }finally{
> > session.close();
> > }
> > }
> > }catch(Exception e){ // This catch is for the first try (the poster's
>
> > insert)
> > session.rollback();
>
> > }finally{
> > session.close();
> > }
> > }
>
> > If p (the instanciated PostMapper) was instanciated before the first
> > commit in order to avoid the multiple getMapper inside the for loop,
> > it does not work.
> > It only works if I do like I wrote above.
> > Is there a way to instanciate all the mapper I will need and then use
> > them inside every session I will open ?
> > The "for" loop is because I want all the insert which do not violate
> > integrity to be saved and other discarded. I need to know witch went
> > wrong and which went good since there if it went good, I add the
> > comments of the current post too in the database.
>
> > Thank you for your answers.
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Mapper class and session

Hunter
That looks correct to me. The commit after poster.insert() is needed
in this case.

On Oct 20, 6:54 pm, rhummy <[hidden email]> wrote:

> Hello,
>
> With your code, if the first post inserted by p.insert(...) has an
> exception, all the next posts will be discarded since the poster
> insertion is cancelled by the session.rollback() inside the for loop.
> For example, if we have 3 posts and the second post raise an
> exception, the third will not be inserted in the database. But the
> poster and the first post will be inserted.
> If a post inserted by p.insert(...) has an exception, I want it
> discarded and I want continue with all the remaining posts.
> At the end, the database will be populated with all the posts which
> have not raised an exception.
>
> Thank you for your help Hunter ! I understand now that the
> session.commit() does not work like in SQL. In SQL, if I commit
> something, it decreases the BEGIN/COMMIT counter. And if the counter
> is equal to 0, I have to begin another BEGIN in order to start a
> transaction.
> Here, in myBatis, it is like sqlMapper.openSession() does a BEGIN,
> session.commit does a COMMIT and a BEGIN, and session.close() does a
> COMMIT. Correct me if I am wrong.
>
> To avoid the "first post issue", I added a "commit" after the
> poster.insert(aposter). Correct me if I have done something wrong :
>
> try {
>   poster.insert(aposter);
>   session.commit(); // commit the poster in order to let subsequent
> post (after the first exception) to be committed (it is because the
> id_poster will not be available to them and an exception will be
> raised even if the post is ok)
>
>   for (....){
>     try{
>       p.insert(....)
>       session.commit();
>     } catch(Exception e){  // catch each p.insert that fails
>       session.rollback();
>     }
>   }
>
> } catch(Exception e){ // catch the failed poster.insert
>
>   session.rollback();
>
>
>
> }  finally {
>   session.close();
> }
>
> On Oct 19, 10:38 pm, Hunter <[hidden email]> wrote:
>
>
>
> > You shouldn't need to close the session after each rollback / commit.
> > You also only need one Mapper for each SqlSession you open.
> > Does something like this work?
>
> > SqlSession session = sqlMapper.openSession()
>
> > PosterMapper poster = session.getMapper(PosterMapper.class);
> > PostMapper p = session.getMapper(PostMapper.class);
>
> > try {
> >   poster.insert(aposter);
>
> >   for (....){
> >     try{
> >       p.insert(....)
> >       session.commit();
> >     } catch(Exception e){  // catch each p.insert that fails
> >       session.rollback();
> >     }
> >   }} catch(Exception e){ // catch the failed poster.insert
>
> >   session.rollback();
>
> > }  finally {
> >   session.close();
> > }
>
> > On Oct 19, 9:12 pm, rhummy <[hidden email]> wrote:
>
> > > Hi all !
>
> > > I would like to know if there is some way to reuse instanciated
> > > mapperclass from another session ?
> > > I have to insert into a table a bunch of data and I do it inside a for
> > > loop.
>
> > > SqlSession session = sqlMapper.openSession();
> > > try{
> > > PosterMapper poster = session.getMapper(PosterMapper.class);
> > > poster.insert(aposter);
> > > session.commit();
>
> > > for (....){
> > > try{
> > > session = sqlMapper.openSession();
> > > PostMapper p = session.getMapper(PostMapper.class);
> > > p.insert(....)
> > > session.commit();}catch(Exception e){
>
> > > session.rollback();
>
> > > }finally{
> > > session.close();
> > > }
> > > }
> > > }catch(Exception e){ // This catch is for the first try (the poster's
>
> > > insert)
> > > session.rollback();
>
> > > }finally{
> > > session.close();
> > > }
> > > }
>
> > > If p (the instanciated PostMapper) was instanciated before the first
> > > commit in order to avoid the multiple getMapper inside the for loop,
> > > it does not work.
> > > It only works if I do like I wrote above.
> > > Is there a way to instanciate all the mapper I will need and then use
> > > them inside every session I will open ?
> > > The "for" loop is because I want all the insert which do not violate
> > > integrity to be saved and other discarded. I need to know witch went
> > > wrong and which went good since there if it went good, I add the
> > > comments of the current post too in the database.
>
> > > Thank you for your answers.