Quantcast
Channel: Ignite Realtime: Message List
Viewing all articles
Browse latest Browse all 12162

Re: Using primary keys instead of getSequence from ofID table

$
0
0

Took the easy and yet extensible way of doing things by creating a set of "sequence-like" tables with foo values.  And then using the generatedKey functionality to essentially get the last_insert_id() from MySQL

 

Sample of a MySQL table additions

# OFFLINE CREATE TABLE ofSeqType19 (

  `id` bigint(20) UNSIGNED AUTO_INCREMENT,

  `foo` tinyint(1), PRIMARY KEY (`id`)

);

 

 

Sample code changes to java.org.jivesoftware.database.SequenceManager.java

private static final String CREATE_SEQ_ID =    "INSERT INTO ofSeqType? (foo) VALUES (1)";          :          :          :    // Drop this into a new function to replace getNextBlock    con = DbConnectionManager.getConnection();    // Get the current ID from the database.    pstmt = con.prepareStatement(CREATE_SEQ_ID.replace("?", Integer.toString(type)));    // pstmt.setInt(1, type); // Doesn't work for table name manipulation    int result = pstmt.executeUpdate();    if (result > 0) {        ResultSet key = pstmt.getGeneratedKeys();        if (key.next()) {            this.maxID = key.getLong(1);            this.currentID = maxID - 1; // Does not respect the blocksize            success = true;        }    }

Viewing all articles
Browse latest Browse all 12162

Trending Articles