-1

Every time receiving specific post request from client, server should produce an unique integer to generate a link(uri: website/question/id/details) and store the integer as index(primary key) in database. I've tried int idForUri = UniqueInteger.uri.incrementAndGet(); But this doesn't work if server reboots. How can I fix this?

class UniqueInteger {
    public static AtomicInteger uri = new AtomicInteger(212043);
}

Add
table article
------------
id int(11) unsigned auto_increment primary key
title varchar(50)
content text
uri varchar(50)

Every specific post request from browser cause server to produce a new uri. User visit that page by the uri, which consists of "website/question/id/details"(id is the primary key). But without knowing the new id, the uri is also unknown. For example, 21000 is currently the max id existed in table article. So insert into article(title, content, uri) values (?, ?, 'website/questions/21001/details') is needed. But server doesn't know the value of 21001 because of auto-increment(maybe leads to a sql select statement before executing insert).

Users by typing specific uri in browser to visit every article page, a HTTP request. Server should know which article to be sent just by the receiving link. So I add id to uri(just like the uri in SO, which is questions/question-id/title).Then by parsing the id filed in uri, server can locate the specific article.
How to fix the design?

user7328234
  • 393
  • 7
  • 23
  • if the server restarts should the unique int generated again? – ΦXocę 웃 Пepeúpa ツ Jun 22 '17 at 05:37
  • Condition is: Every time receiving specific post request from client, then produce an unique integer. It's better to produce unique integer and its value not too large(less than 1000,000,00). – user7328234 Jun 22 '17 at 05:40
  • I think you are going about this back-to-front - insert into the DB and use the auto-generated key – Scary Wombat Jun 22 '17 at 05:41
  • @ΦXocę웃Пepeúpaツ Condition is: Every time receiving specific post request from client, server will produce an unique integer .It's better to produce unique integer and its value not too large(less than 1000,000,00). – user7328234 Jun 22 '17 at 05:41
  • @ScaryWombat. With auto-generated key, it leads to add a select statement in server to produce the link. Because server doesn't know the value before inserting the link into DB(index is auto-generated), so it needs a select. – user7328234 Jun 22 '17 at 05:44
  • see this [answer](https://stackoverflow.com/a/1376241/2310289) – Scary Wombat Jun 22 '17 at 05:47
  • @ScaryWombat I think you may not understand my confusion. For example, table article (id auto-increment, uri varchar(50), title varchar(50), content text). Before execute sql statement `insert`, the uri column should know the value of `id` first. Because the uri is filled with (website/id/details) – user7328234 Jun 22 '17 at 05:54
  • see my answer below – Scary Wombat Jun 22 '17 at 05:56
  • 1
    As I read your description the `uri`.column is fully redundant. Why do you need it when the URI can be constructed from the other data? – piet.t Jun 22 '17 at 06:26
  • @piet.t I've added more details. – user7328234 Jun 22 '17 at 07:01
  • 1
    But your addition does not answer: why do you need to store the URI in the database? When a request comes in you know the incoming URI and extract the id. When you need to create a link to an article you read the id from the database and construct the URI in your application. – piet.t Jun 22 '17 at 07:05
  • 1
    @piet.t same thing as I am saying in my answer – Scary Wombat Jun 22 '17 at 07:07

2 Answers2

3

Use a SEQUENCE or equivalent concept from the database.

Henry
  • 42,982
  • 7
  • 68
  • 84
  • I've added more details in my question. – user7328234 Jun 22 '17 at 06:26
  • 1
    The answer still applies: get a new ID value from the database, then insert the record. Also, why do you need to store the URI? Isn't it determined only by knowing the ID. – Henry Jun 22 '17 at 06:29
  • Yes, it's detemined only by knowing the ID. Users by typing specific uri in browser to visit every article page, a HTTP request. Server should know which article to be sent just by the receving link. So I add id to uri(Just like the uri in SO, it's questions/question-id/title), then by parsing the id filed in uri, server can locate the specific article. – user7328234 Jun 22 '17 at 06:36
1

Based upon your comments, the easiest way would be just to let the DB insert use auto-generated keys as per normal and

change the getUri method to append or insert the id field into it

return String.format ("website/question/%d/details", id);
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Thanks, but the uri is also a column in table article. So you can't execute `insert` before knowing the id.I will add more details in my question to make it clear. – user7328234 Jun 22 '17 at 06:00
  • No I am saying that the url in the DB should not even contain the `id` The **getter** on your EntityBean should do the logic – Scary Wombat Jun 22 '17 at 06:18
  • I'm still confused about it. When user types a uri in browser, how can server locate specific item in table article without id in uri. – user7328234 Jun 22 '17 at 07:02
  • Do you have an EnityBean for your article record? Maybe you are selecting all (or some) and displaying in a HTML table. When you read the record you have the id - use it to construct your URL – Scary Wombat Jun 22 '17 at 07:06