-3
    public class EmployeeServiceImpl implements EmployeeService  {
        static int uniqueIndex=1000;
        static int  uniqueid=uniqueIndex++;
    
    
    public String setEmpCode()
        {
            String prefixName="AWL";
            return prefixName+uniqueid;
            
        }
        }

<div class="col-sm-9">
<input type="text" id="emp_code" name="emp_code" placeholder="Code"
th:field="*{emp_code}" class="form-control"  th:value="${emp.emp_code}" autofocus disabled="disabled">
</div>

in emp_code incremented value get stored like AWL1000, AWL1001 , but when i restart the apache service in spring java, again Emp_code get reset to AWL1000 instead of AWL1002,

How can i keep those incremented value even after apache service restart?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
user2715085
  • 93
  • 1
  • 2
  • 12
  • 1
    Why would you expect a server restart to remember the last variable state? You need to persist the value somewhere and load it during startup. – luk2302 Oct 03 '21 at 13:26
  • My value getting keep incremented properly , but when i restart the apache service , Value get set to initial Value again instead of incremented value.Thats what i want to prevent , how to get incremented value even after apache service restart – user2715085 Oct 03 '21 at 13:26

1 Answers1

1

even after apache service restart.

Then you need data persistence like a file or a database. Otherwise, your variable is stored in memory, and reset to 1000 each time the JVM restarts

Database would be preferred if you would also like your application to be highly available (web clients will always be able to reach some server instance, behind a load balancer / proxy)

Plus, if you used particular databases (RDBMS), it can generate auto-incrementing IDs for you

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245