У меня есть проект, использующий последнюю версию Spring 5 с последней версией Hibernate 5. У меня есть метод в компоненте, аннотированный @Async @Scheduled (fixedDelay = 500).Тело этой функции пустое:
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
@Component
public class SchedulerBean implements Serializable {
private transient static final Logger LOG = LogManager.getLogger();
@PersistenceContext(type = PersistenceContextType.TRANSACTION)
private EntityManager entityManager;
@Async
@Scheduled(fixedDelay = 500)
public void spoolerSender() {
}
}
Каждый раз, когда этот метод запускается, даже сейчас он пустой, он запрашивает базу данных, вот дамп журнала запросов:
+----------------------------+------------------------------------+-----------+-----------+--------------+---------------------------------------------------------------------------------------+
| event_time | user_host | thread_id | server_id | command_type | argument |
+----------------------------+------------------------------------+-----------+-----------+--------------+---------------------------------------------------------------------------------------+
| 2018-06-13 15:03:48.185014 | root[root] @ localhost [] | 39544 | 0 | Quit | |
| 2018-06-13 15:03:48.281757 | [root] @ localhost [127.0.0.1] | 39545 | 0 | Connect | root@localhost as anonymous on hibernate_test |
| 2018-06-13 15:03:48.281951 | root[root] @ localhost [127.0.0.1] | 39545 | 0 | Query | set autocommit=1, sql_mode = concat(@@sql_mode,',STRICT_TRANS_TABLES') |
| 2018-06-13 15:03:48.282080 | root[root] @ localhost [127.0.0.1] | 39545 | 0 | Query | SELECT @@max_allowed_packet,@@system_time_zone,@@time_zone,@@auto_increment_increment |
| 2018-06-13 15:03:48.282473 | root[root] @ localhost [127.0.0.1] | 39545 | 0 | Query | set autocommit=0 |
| 2018-06-13 15:03:48.282875 | root[root] @ localhost [127.0.0.1] | 39545 | 0 | Query | set autocommit=1 |
| 2018-06-13 15:03:48.283170 | root[root] @ localhost [127.0.0.1] | 39545 | 0 | Quit | |
| 2018-06-13 15:03:48.782109 | [root] @ localhost [127.0.0.1] | 39546 | 0 | Connect | root@localhost as anonymous on hibernate_test |
| 2018-06-13 15:03:48.782307 | root[root] @ localhost [127.0.0.1] | 39546 | 0 | Query | set autocommit=1, sql_mode = concat(@@sql_mode,',STRICT_TRANS_TABLES') |
| 2018-06-13 15:03:48.782438 | root[root] @ localhost [127.0.0.1] | 39546 | 0 | Query | SELECT @@max_allowed_packet,@@system_time_zone,@@time_zone,@@auto_increment_increment |
| 2018-06-13 15:03:48.782881 | root[root] @ localhost [127.0.0.1] | 39546 | 0 | Query | set autocommit=0 |
| 2018-06-13 15:03:48.783379 | root[root] @ localhost [127.0.0.1] | 39546 | 0 | Query | set autocommit=1 |
| 2018-06-13 15:03:48.783855 | root[root] @ localhost [127.0.0.1] | 39546 | 0 | Quit | |
| 2018-06-13 15:03:49.283183 | [root] @ localhost [127.0.0.1] | 39547 | 0 | Connect | root@localhost as anonymous on hibernate_test |
| 2018-06-13 15:03:49.283513 | root[root] @ localhost [127.0.0.1] | 39547 | 0 | Query | set autocommit=1, sql_mode = concat(@@sql_mode,',STRICT_TRANS_TABLES') |
| 2018-06-13 15:03:49.283687 | root[root] @ localhost [127.0.0.1] | 39547 | 0 | Query | SELECT @@max_allowed_packet,@@system_time_zone,@@time_zone,@@auto_increment_increment |
| 2018-06-13 15:03:49.284119 | root[root] @ localhost [127.0.0.1] | 39547 | 0 | Query | set autocommit=0 |
| 2018-06-13 15:03:49.284678 | root[root] @ localhost [127.0.0.1] | 39547 | 0 | Query | set autocommit=1 |
| 2018-06-13 15:03:49.285206 | root[root] @ localhost [127.0.0.1] | 39547 | 0 | Quit | |
| 2018-06-13 15:03:49.783007 | [root] @ localhost [127.0.0.1] | 39548 | 0 | Connect | root@localhost as anonymous on hibernate_test |
| 2018-06-13 15:03:49.783202 | root[root] @ localhost [127.0.0.1] | 39548 | 0 | Query | set autocommit=1, sql_mode = concat(@@sql_mode,',STRICT_TRANS_TABLES') |
| 2018-06-13 15:03:49.783377 | root[root] @ localhost [127.0.0.1] | 39548 | 0 | Query | SELECT @@max_allowed_packet,@@system_time_zone,@@time_zone,@@auto_increment_increment |
| 2018-06-13 15:03:49.783758 | root[root] @ localhost [127.0.0.1] | 39548 | 0 | Query | set autocommit=0 |
| 2018-06-13 15:03:49.784199 | root[root] @ localhost [127.0.0.1] | 39548 | 0 | Query | set autocommit=1 |
| 2018-06-13 15:03:49.784719 | root[root] @ localhost [127.0.0.1] | 39548 | 0 | Quit | |
| 2018-06-13 15:03:50.283336 | [root] @ localhost [127.0.0.1] | 39549 | 0 | Connect | root@localhost as anonymous on hibernate_test |
| 2018-06-13 15:03:50.283520 | root[root] @ localhost [127.0.0.1] | 39549 | 0 | Query | set autocommit=1, sql_mode = concat(@@sql_mode,',STRICT_TRANS_TABLES') |
| 2018-06-13 15:03:50.283740 | root[root] @ localhost [127.0.0.1] | 39549 | 0 | Query | SELECT @@max_allowed_packet,@@system_time_zone,@@time_zone,@@auto_increment_increment |
| 2018-06-13 15:03:50.284145 | root[root] @ localhost [127.0.0.1] | 39549 | 0 | Query | set autocommit=0 |
| 2018-06-13 15:03:50.284600 | root[root] @ localhost [127.0.0.1] | 39549 | 0 | Query | set autocommit=1 |
| 2018-06-13 15:03:50.284939 | root[root] @ localhost [127.0.0.1] | 39549 | 0 | Quit | |
| 2018-06-13 15:03:50.784405 | [root] @ localhost [127.0.0.1] | 39550 | 0 | Connect | root@localhost as anonymous on hibernate_test |
| 2018-06-13 15:03:50.784703 | root[root] @ localhost [127.0.0.1] | 39550 | 0 | Query | set autocommit=1, sql_mode = concat(@@sql_mode,',STRICT_TRANS_TABLES') |
| 2018-06-13 15:03:50.784955 | root[root] @ localhost [127.0.0.1] | 39550 | 0 | Query | SELECT @@max_allowed_packet,@@system_time_zone,@@time_zone,@@auto_increment_increment |
| 2018-06-13 15:03:50.785599 | root[root] @ localhost [127.0.0.1] | 39550 | 0 | Query | set autocommit=0 |
| 2018-06-13 15:03:50.786197 | root[root] @ localhost [127.0.0.1] | 39550 | 0 | Query | set autocommit=1 |
| 2018-06-13 15:03:50.786582 | root[root] @ localhost [127.0.0.1] | 39550 | 0 | Quit | |
| 2018-06-13 15:03:51.141919 | [root] @ localhost [] | 39551 | 0 | Connect | root@localhost as anonymous on |
| 2018-06-13 15:03:51.142045 | root[root] @ localhost [] | 39551 | 0 | Query | select @@version_comment limit 1 |
| 2018-06-13 15:03:51.142223 | root[root] @ localhost [] | 39551 | 0 | Query | SET GLOBAL general_log="OFF" |
+----------------------------+------------------------------------+-----------+-----------+--------------+---------------------------------------------------------------------------------------+
Это происходит из-за EntityManager и / или Transactional?Разве этот класс не должен быть создан только один раз?