HelloDbController.java 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package hello.web;
  2. import hello.domain.*;
  3. import java.io.*;
  4. import java.util.*;
  5. import java.util.concurrent.*;
  6. import javax.servlet.http.*;
  7. import org.hibernate.Session;
  8. import org.hibernate.Transaction;
  9. import org.springframework.http.*;
  10. import org.springframework.http.converter.*;
  11. import org.springframework.http.converter.json.*;
  12. import org.springframework.http.server.*;
  13. import org.springframework.stereotype.*;
  14. import org.springframework.web.bind.annotation.*;
  15. @Controller
  16. public class HelloDbController
  17. {
  18. private static final int DB_ROWS = 10000;
  19. @RequestMapping(value = "/db", produces = "application/json")
  20. @ResponseBody
  21. public World[] index(Integer queries)
  22. {
  23. if (queries == null)
  24. {
  25. queries = 1;
  26. }
  27. final World[] worlds = new World[queries];
  28. final Random random = ThreadLocalRandom.current();
  29. final Session session = HibernateUtil.getSessionFactory().openSession();
  30. for(int i = 0; i < queries; i++)
  31. {
  32. worlds[i] = (World)session.byId(World.class).load(random.nextInt(DB_ROWS) + 1);
  33. }
  34. session.close();
  35. return worlds;
  36. }
  37. }