-8

I used int as PK in Java application, now it has reached the max int value (2 billions), even in DB it can store the number more than it. but java int is only able to hold around 2 billions.

I am unable to change int to long to align to DB. because it's huge effort.

except this, anybody have any approach?

6
  • 7
    You've already answered your question.
    – Zutty
    Mar 14, 2013 at 16:08
  • Last week, the very same question was asked. Let me see if I find it.
    – Ingo
    Mar 14, 2013 at 16:09
  • Are you using every integer from 1 through to Integer.MAX? Can you reset your autoincrement to reuse numbers that you've used previously? Mar 14, 2013 at 16:09
  • 1
    You have several options. 1. change to use long in java. This is the best option you have. 2. Change to use Sting as the pk value in java. 3. Find a new job and let somebody else change to use long in java.
    – DwB
    Mar 14, 2013 at 16:10
  • Why don't you use the 2 billion values between 2 billion and 4 billion and int can represent? (Note that values >= 2^31 will be interpreted as negative ones in java, but this doesn't matter as long as you use them only as keys.)
    – Ingo
    Mar 14, 2013 at 16:15

6 Answers 6

6

The maximum value for an Integer in Java is 2,147,483,647. If you need a bigger number, you'll have to change to a long. You could also use the negative range of the Integer, but if you've already hit the maximum, the likelihood is that you'll run out of room pretty soon.

However, if you don't have 2 billion elements in the DB, you could reuse the unused primary keys. This would probably be inefficient, because you'd have to search for unused keys.

I'd suggest just going through the effort of changing the code. Putting in the effort now will pay off in the long run.

3

I am unable to change int to long to align to DB. because it's huge effort.

You have no alternatives in the long term. Start coding.

Actually, if you are methodical about it, you will probably find that it is not a huge effort at all. Java IDEs are good for helping you with this sort of change.


@jjnguy suggested you let the keys wrap around to negative. That would give you 2 billion or so extra keys, but:

  • you will probably use the second 2 billion faster quicker than the first 2 billion, and
  • it is possible that your application (or the database) depends on keys always increasing.

So I would avoid that, unless roll-over was imminent.

9
  • It's the interaction with other software components that could make this a hard thing, actually.
    – Ingo
    Mar 14, 2013 at 16:16
  • Yes well ... the further these ids go, the worse it gets. But you would kind of hope that external components are not dependent on the representation of the database pks. The good thing is that it is unlikely is going to try to do anything other than pass pk values and compare them. Arithmetic, etc is unlikely ...
    – Stephen C
    Mar 14, 2013 at 16:23
  • My application is unable to handle negative number. so can switch the negative part to the number bigger than 2 billion? as int can hold 4 billions number. Mar 14, 2013 at 16:29
  • No you can't. The Java int type is signed, with 2,000,000,000 positive values and 2,000,000,000 negative values (roughly). You can't change magically make Java int unsigned.
    – Stephen C
    Mar 14, 2013 at 16:49
  • @user2166163 It's not numbers, it's just bit patterns, and as such neither negative nor positive, despite what people tell you about signed vs. unsigned. The fact is that you can use int to make 2^32 different keys. But, to be sure, if you interpret it as number, and your program can't handle negative numbers, well, then you did it wrong. At least you can perhaps use long in the parts of your program that uses the keys as numbers?
    – Ingo
    Mar 14, 2013 at 17:06
0

I'm assuming you haven't used negative values for the IDs. If that's the case, you can let the value overflow to negative values. This will give you some time to refactor your code to use a long to store your data.

1
  • True, an int can have more than 4 billions of different values.
    – Ingo
    Mar 14, 2013 at 16:12
0

you need just to replace int with uint, long and etc. there is no other way

0

Everyone here suggests to change to long. This seems to me also the most straightforward approach. However, you ask for a different one.

You could also create another column, with long values, copy the value from the PK, and setting that henceforth as the PK. Although I would see this technically as more work, maybe for your situation it is better, and strictly it is an answer to your question of another approach.

(Maybe you have some sort of sharded situation with thousand shards which you can´t possibly all in the same time swap over). Either way be very careful! Run tests!

-1

Use BigInteger class in Java. You can get large and by large i mean really very large values. Please refer to this link: How to use BigInteger?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.