I am using Spring Boot and JdbcTemplate
in my application. I am trying to implement timeout for select query but its not working.
My query takes more time than timeout time but still its not giving timeout exception.
@Service
@Slf4j
public class SchedulerService
{
@Autowired
UserService userExportService;
@Autowired
private userExportDao userExportDao;
@Value("${queryTest}")
private String queryFetchByExportFlagCustom;
@Scheduled(fixedDelay=10000)
public void triggerUserExport() {
List<UserExportCustom> userList;
try {
userList = userExportDao.findByExportFlag(0, queryFetchByExportFlagCustom);
userExportService.exportUsers(userList, schedulerCount);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Repository
@Slf4j
public class UserExportDao extends JdbcDaoImpl<UserExportCustom, Long>
{
@Autowired
BeanPropertyRowMapper<UserExportCustom> userExportCustomRowMapper;
@Transactional(readOnly = true, timeout = 1)
public List<UserExportCustom> findByExportFlag(Integer exportFlag, String query)
{
List<UserExportCustom> userExportCustomList = null;
try
{
SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("exportFlag", exportFlag, Types.INTEGER);
userExportCustomList = namedParameterJdbcTemplate.query(query, namedParameters,userExportCustomRowMapper);
}
catch (Exception e)
{
e.printStackTrace();
log.error("Error in findByExportFlag: \n" + e);
}
return userExportCustomList;
}
}
public class JdbcDaoImpl<T, ID> implements JdbcDao<T, ID> {
@Autowired
protected JdbcTemplate jdbcTemplate;
@Autowired
protected NamedParameterJdbcTemplate namedParameterJdbcTemplate;
@Override
public List<T> findAll() {
throw new IllegalStateException();
}
@Override
public T save(T api) {
throw new IllegalStateException();
}
@Override
public T update(T api) {
throw new IllegalStateException();
}
@Override
public T saveOrUpdate(T api) {
throw new IllegalStateException();
}
@Override
public T findOne(String unique) {
throw new IllegalStateException();
}
@Override
public T findOneById(ID id) {
throw new IllegalStateException();
}
@Override
public void delete(ID id) {
throw new IllegalStateException();
}
}
If query take more than 1 second than it should give timeout exception but it does not.
try{}catch{} will lead @Transactional to fail ,remove try catch