I would like to add an option to select sqlSession when executing the very long query.

14 views
Skip to first unread message

HeeGu Lee

unread,
Mar 17, 2020, 5:46:07 AM3/17/20
to mybatis-user
Hi, Mybatis!

My predecessor made a query that was close to 1000 lines and left the company.
The problem was a query with transactions tied up, but I only had to service the results of doing a simple query.
The solution is simple. Just create a new slave mapper and copy it.
However, this causes a situation where the source needs to be modified twice.
So I thought that I would like to have the option to force SqlSession to be specified when executing a query in Mybatis.
Basically, it uses A session, but puts the option as below to use B session connected to Slave DB.

ex) 

A.java

@Options(sqlSessionRef="slaveSqlSession")
public int updateVeryLongQuery();

A.xml
<select id="updateVeryLongQuery">
... very long query ...
</select>

Is there any way to do this?

Thank you for reading.

Have a nice day! And please be safe from corona-19.

Iwao AVE!

unread,
Mar 19, 2020, 11:32:12 AM3/19/20
to mybatis-user
Hello HeeGu,

Adding sqlSessionRef to @Options may be difficult, but there might be a decent solution.
Could you elaborate on the problem you are having?
I understand that there was/is a very large query, but I am not sure what you needed to achieve (=requirement).

Regards,
Iwao

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/c3a2f166-0305-4842-919a-beccd2459502%40googlegroups.com.

HeeGu Lee

unread,
Mar 20, 2020, 3:21:20 AM3/20/20
to mybatis-user
Hello Iwao!

In our project, there is a query to get the balance of points that the customer has, so we set it to read from the Slave DB.

However, because the transaction takes place in the logic that uses points, even if you try to return the balance by calling the logic at the end, the balance has not been committed and the balance does not change even when read from the slave.

I can also copy the query to the master mapper and read it in the master DB session.
However, the query was a bit complicated, so it was difficult to fix it twice each time, so I thought about a good way. Then I thought about what I could do if I could specify SqlSession.

Is there a good way to do this?

2020년 3월 20일 금요일 오전 12시 32분 12초 UTC+9, Iwao AVE! 님의 말:
To unsubscribe from this group and stop receiving emails from it, send an email to mybati...@googlegroups.com.

Iwao AVE!

unread,
Mar 20, 2020, 11:43:10 AM3/20/20
to mybatis-user
So, you want to execute the query sometimes against master, other times against slave, is that correct?
Then you can move the complex query to a new mapper and register the mapper to both SqlSessionFactory.

masterSqlSessionFactory.getConfiguration().addMapper(SharedMapper.class); 
slaveSqlSessionFactory.getConfiguration().addMapper(SharedMapper.class);

When you want to execute the query against master, get the mapper from the master's SqlSession.

try (SqlSession sqlSession = masterSqlSessionFactory.openSession()) {
  SharedMapper mapper = sqlSession.getMapper(SharedMapper.class);
  int result = mapper.updateVeryLongQuery()
}

Regards,
Iwao

To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/b00ec238-c9a2-49a7-a935-169d56d0bedd%40googlegroups.com.

HeeGu Lee

unread,
Mar 22, 2020, 9:37:16 PM3/22/20
to mybatis-user
It's really amazing that you can add a mapper by taking the configuration out of the SqlSessionFactory.
I'll do it right now.
Thanks for the help.

Have a nice day.

2020년 3월 21일 토요일 오전 12시 43분 10초 UTC+9, Iwao AVE! 님의 말:
Reply all
Reply to author
Forward
0 new messages