1 29 30 package com.caucho.amber.idgen; 31 32 import com.caucho.amber.manager.AmberConnection; 33 import com.caucho.amber.manager.AmberPersistenceUnit; 34 import com.caucho.amber.type.GeneratorTableType; 35 import com.caucho.util.L10N; 36 import com.caucho.util.Log; 37 38 import javax.sql.DataSource ; 39 import java.sql.Connection ; 40 import java.sql.PreparedStatement ; 41 import java.sql.ResultSet ; 42 import java.sql.SQLException ; 43 import java.sql.Statement ; 44 import java.util.logging.Logger ; 45 46 49 public class AmberTableGenerator extends IdGenerator { 50 private static final L10N L = new L10N(AmberTableGenerator.class); 51 private static final Logger log = Log.open(AmberTableGenerator.class); 52 53 private AmberPersistenceUnit _manager; 54 private GeneratorTableType _table; 55 private String _name; 56 57 private String _selectSQL; 58 private String _updateSQL; 59 60 private boolean _isInit; 61 62 65 public AmberTableGenerator(AmberPersistenceUnit manager, 66 GeneratorTableType table, 67 String name) 68 { 69 _manager = manager; 70 _table = table; 71 _name = name; 72 } 73 74 77 public long allocateGroup(AmberConnection aConn) 78 throws SQLException 79 { 80 int groupSize = getGroupSize(); 81 82 int retry = 5; 83 84 Connection conn = aConn.getConnection(); 86 PreparedStatement selectStmt = conn.prepareStatement(_selectSQL); 87 PreparedStatement updateStmt = conn.prepareStatement(_updateSQL); 88 89 selectStmt.setString(1, _name); 90 updateStmt.setString(2, _name); 91 92 while (retry-- > 0) { 93 ResultSet rs = selectStmt.executeQuery(); 94 if (rs.next()) { 95 long value = rs.getLong(1); 96 rs.close(); 97 98 updateStmt.setLong(1, value + groupSize); 99 updateStmt.setLong(3, value); 100 101 if (updateStmt.executeUpdate() == 1) 102 return value; 103 } 104 rs.close(); 105 } 106 107 throw new SQLException (L.l("Can't allocate id from table '{0}'", 108 _table.getTable().getName())); 109 } 110 111 114 public void init(AmberPersistenceUnit amberPersistenceUnit) 115 throws SQLException 116 { 117 if (_isInit) 118 return; 119 _isInit = true; 120 121 _selectSQL = ("select " + _table.getValueColumn() + 122 " from " + _table.getTable().getName() + 123 " where " + _table.getKeyColumn() + "=?"); 124 125 _updateSQL = ("update " + _table.getTable().getName() + 126 " set " + _table.getValueColumn() + "=?" + 127 " where " + _table.getKeyColumn() + "=? " + 128 " and " + _table.getValueColumn() + "=?"); 129 130 DataSource ds = amberPersistenceUnit.getDataSource(); 131 Connection conn = ds.getConnection(); 132 try { 133 try { 134 PreparedStatement pstmt = conn.prepareStatement(_selectSQL); 135 136 pstmt.setString(1, _name); 137 138 ResultSet rs = pstmt.executeQuery(); 139 if (rs.next()) { 140 rs.close(); 141 return; 142 } 143 } catch (SQLException e) { 144 } 145 146 String sql = ("INSERT INTO " + _table.getTable().getName() + " (" + 147 _table.getKeyColumn() + "," + 148 _table.getValueColumn() + ") VALUES " + 149 "('" + _name + "', 1)"); 150 151 Statement stmt = conn.createStatement(); 152 stmt.executeUpdate(sql); 153 stmt.close(); 154 } finally { 155 conn.close(); 156 } 157 } 158 } 159 | Popular Tags |