How to put Row level lock in java while inserting a row in MSSQL table ?
Hi Lakshmi Ramachandran,
If U r using
EJB no need worry about this things but
everytime U have to close connection(ResultSet etc..)
otherwise it gives the exception.
Example:
public long insert(UserTB myrecord) throws CreateException {
Connection dbConnection=null;
Statement st=null;
ResultSet rs=null;
PreparedStatement ps=null;
String strQry=null;
try{
dbConnection = getConnection();
st=dbConnection.createStatement();
rs=st.executeQuery("SELECT USERID FROM SEA_USER WHERE USERID='"+pk+"'");
if(!rs.next()){
strQry = " INSERT INTO USER (USERID, ..... ) " +
" VALUES (?, ........ )";
ps = dbConnection.prepareStatement(strQry);
ps.setString(1, myrecord.getUserID());
.......
}//if(!rs.next()
else{
throw new SQLException("User ID already exists...");
}
return myrecord.getUserID() ;
}catch(SQLException se){
throw new CreateException(se.getMessage());
}finally{
//This is one very importent
try {
if (rs!=null) rs.close();
if( st != null) st.close();
if (ps != null ) ps.close();
if (dbConnection != null ) dbConnection.close();
}catch(Exception e) {
throw new CreateException("EXCEPTION WHEN CLOSE RESOURCE IN INSERT ");
}
}
}//EOF INSERT
If U r not using EJB U have to create connectionpool
using Vector,Threds...etc., If U need I will send
code.