日期:2014-05-20  浏览次数:21120 次

java怎么给数据库加行锁(事务处理)
用的数据库是Informax 9.3 FC。
Java code

try {
    this.connection.setAutoCommit(false);
    //操作数据库
    this.connection.commit();
} catch(Exception e) {
    this.connection.rollback();
}


使用如上代码会把整张表给锁住了,求高人指点怎么给数据库加行锁。。
PS:数据库本身的begin work; roll work; commit work;貌似不会锁整张表。

------解决方案--------------------
貌似不同数据库不一样吧。。

我知道Oracle用select...for update
------解决方案--------------------
SELECT * FROM table ROWLOCK WHERE id = 1

------解决方案--------------------
程序用SQL语句是操作数据库的表。不是行
------解决方案--------------------
http://www.j6a.ru/_jdbc_odbc_prepared_statement_8java_source.html
Java code

01498         //--------------------------------
01499         // addBatch()
01500         // adds a set Of Parameters for executeBatch Updates.
01501         //--------------------------------
01502 
01503         public void addBatch() throws SQLException
01504         {
01505                 if (OdbcApi.getTracer().isTracing ()){
01506                         OdbcApi.getTracer().trace ("*PreparedStatement.addBatch");
01507                 }
01508 
01509             try
01510             {
01511                 int storedSize;
01512 
01513                 //get from storage Vector and add values to it.
01514                 batchSqlVec = myConnection.getBatchVector(this);
01515 
01516                 // If adding parameter for the first time
01517                 // create the vector to hold them.
01518                 if ( batchSqlVec == null )
01519                 {
01520                     batchSqlVec = new Vector(5,10);
01521 
01522                     storedSize = 0;
01523                 }
01524                 else
01525                 {
01526                     storedSize = batchSqlVec.size();    
01527                 }
01528 
01529                 Object[] addBatchParams = arrayParams.getStoredParameterSet();
01530                 int[] addBatchParamsIdx = arrayParams.getStoredIndexSet();
01531 
01532                 int batchDataLen        = addBatchParams.length;
01533                 int batchIdxLen         = addBatchParamsIdx.length;
01534 
01535                 if ( batchIdxLen == numParams )
01536                 {
01537                         batchSqlVec.addElement(addBatchParams);                            
01538 
01539                         myConnection.setBatchVector(batchSqlVec, this);
01540 
01541                         arrayParams.storeRowIndex( storedSize, addBatchParamsIdx );
01542 
01543                         batchOn = true;
01544                 }
01545                 else if ( storedSize == 0 )
01546                 {
01547                         throw new SQLException("Parameter-Set has missing values.");
01548                 }
01549                 else
01550                 {
01551                         //myConnection.setBatchVector(batchSqlVec, this);
01552                         batchOn = true;
01553                 }
01554                                                                 
01555             }
01556             catch (NullPointerException e)
01557             {
01558                   //throw new SQLException("Parameter Set has missing values");
01559                   batchOn = false;
01560             }    
01561         
01562         }
01563

------解决方案--------------------
没用过这个数据库。。alter table table_name lock mode row这样么
http://www.cntxk.com/catanews/56/info8355.html