DotHub
DotHub

Reputation: 1

How to manipulate a parameter in mySQL according to server time

i have built a RESTful server with a database with Spring/hibernate/mySQL.

i have a table of a bank savings account that has 3 savings balance columns.

for example:

account_id | a_savings | b_savings | c_savings  
1          | 100       | 200       | 300  

i want that each day (or month) , each savings account will add 0.01%(or other amount) to its value automatically according to the server/current time.

how can i do it?

Upvotes: 0

Views: 31

Answers (2)

DotHub
DotHub

Reputation: 1

Solved it with scheduling tasks:

@Component
public class ScheduledTasks {

private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

@Autowired
private SavingsAccountDao savingsAccountDao;

/* each day at 8 am */
@Scheduled(cron = "0 0 8 * * *")
public void increaseSavingsAccount() {
    log.info("Savings Accounts Updated", dateFormat.format(new Date()));

    /* get all savings accounts and increase balance according to the interest */
    List<SavingsAccount> savingsAccountList = savingsAccountDao.findAll();
    savingsAccountList.forEach((sa) -> {

        /* 39% interest in 12 months */
        sa.setASavingsBalance(sa.getASavingsBalance().multiply(new BigDecimal(1.0009)));
    });
}

}

Upvotes: 0

Mahbub Alam
Mahbub Alam

Reputation: 36

Write a method that pauses for an amount of time than call a method that

  1. load the current value of a field by SQL
  2. increase the values of a field by calculating its value, value = value+(value*0.01) then use SQL to update the value.

For pause in java use TimeUnit.MINUTES.sleep(2); of java.util.concurrent.TimeUnit here 2 stands for 2 Minutes . You can also use DAYS/HOURS TimeUnit.DAYS.sleep(1);

Upvotes: 1

Related Questions