Java servlets: the best way to do multiple inserts and stop tables?

I am currently executing the following segment of code in Java servo using the suggested PareparedStatement class. He is currently entering inventory data into the accounting system's SQL Server database. I have two problems.

  • Since insert statements insert data into 3 different tables, should I move those sql statements into a tsql procedure call on the database and use a transaction to lock the table?
  • Whenever the user is presented with the accounting software inventory screen, the last 2 plug-in assertions fail. I am assuming that the software client is locking the table while the user is opening the screen.

here is the code:

try {   

        con = java.sql.DriverManager.getConnection(getConnectionUrl()); 
        //get next itemkey 
        CallableStatement cstmt = con.prepareCall("{call spGetNextSurrogateKey (?,?)}");
        cstmt.setString("iTableName","timitem");
        cstmt.registerOutParameter("oNewKey", java.sql.Types.INTEGER);
        cstmt.execute(); 
        int rs4 = cstmt.getInt(2);
        cstmt.close();

        String query = "insert into timitem (itemkey, AllowCostOvrd,AllowDecimalQty,AllowDropShip,AllowPriceOvrd,AllowRtrns,AvailForSale,CompanyID,CreateDate,CreateUserID,CreateType,DateEstab,DfltSaleQty,HazMat,InclOnPackList,InternalLongDesc,IntrnlDeliveryReq,ItemClassKey,ItemID,ItemSubType,ItemType,MinGrossProfitPct,MinSaleQty,PerUsageOrdLimit,PriceSeq,PriceUnitMeaskey,PurchProdLineKey,PurchUnitMeasKey,RcptReq,RestockRate,SaleMultiple,SalesUnitMeasKey,Seasonal,ShelfLife,Status,STaxClasskey,StdPrice,StdUnitCost,StockUnitMeasKey,SubjToTradeDisc,TargetMargin,TrackMeth,UpdateCounter,ValuationMeth,WarrantyDays,WarrantyProvider) values ( '" +rs4 + "', 0,0,1,1,1,1,'ens','" + DateFormat.format(Date) + "','admin',1,'" + DateFormat.format(Date) + "',1,0,1,0,0,"+itemclasskey+",'" + partnumber + "',1,5,0,1,0,0,112,"+PurchProdLineKey+","+UnitMeasKey+",1,0,0,112,0,0,1,12,"+ itemlistprice + ","+itemcost + ",112,0,0,2,0,5,0,0)";
        PreparedStatement pstmt = con.prepareStatement(query); 
        pstmt.executeUpdate(); 
        pstmt.close();            

                String query_descrip = "insert into timitemdescription (itemkey, languageid, longdesc, shortdesc) values ('" + rs4 + "', 1033, '" + itemdescription + "','"+ "_" + "')";

                PreparedStatement pstmt2 = con.prepareStatement(query_descrip); 
        pstmt2.executeUpdate();                         
                pstmt2.close();

                String query_UOM = "insert into timItemUnitOfMeas (itemkey, TargetUnitMeasKey, conversionfactor, unitvolume, unitweight,upc,useforpurchases,useforsales,usestdconv) values ('" + rs4 + "', "+UnitMeasKey+", '1',0,0,NULL,0,0,0)";

                PreparedStatement pstmt3 = con.prepareStatement(query_UOM); 
        pstmt3.executeUpdate();                         
                pstmt3.close();


}catch(java.sql.SQLException e){ e.printStackTrace(); }     //end try 

      

any suggestions? thanks in advance.

0


a source to share


2 answers


Working with JDBC at a low level, each statement is automatically captured when it is executed. To execute multiple statements in a single transaction, call setAutoCommit(false)

in Connection

and end the call commit()

on the connection. If it fails, call rollback()

.



More common nowadays are persistence mechanisms such as JPA, transaction management, framework or container work. This infrastructure is perfectly capable of handling common transaction scenarios.

+3


a source


I would assume that you are not doing this in a servlet. Move the code to a persistence layer that you can test offline, without a servlet container.

I would also recommend Spring and JPA . Dividing your application so that web pages and persistence levels are not the same is a good idea.



http://java.sun.com/developer/technicalArticles/J2EE/jpa/ http://www.springsource.org/

0


a source







All Articles