[英]java servlets: better way to do multiple inserts and locking tables?
I'm currently running the follow code segment in a java servlet using the suggested PareparedStatement class. 我目前正在使用建议的PareparedStatement类在Java servlet中运行以下代码段。 It is currently inserting inventory data into a SQL Server database of an accounting system.
当前,它正在将库存数据插入记帐系统的SQL Server数据库中。 I have two problems.
我有两个问题。
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.
提前致谢。
Working with JDBC at a low level, each statement is automatically committed when it is executed. 在底层使用JDBC时,每个语句在执行时都会自动提交。 To execute multiple statements in a single transaction, invoke
setAutoCommit(false)
on the Connection
, and finish the unit of work with a call to commit()
on the connection. 要在单个事务中执行多个语句,请在
Connection
上调用setAutoCommit(false)
,并通过对连接上的commit()
进行调用来完成工作单元。 If there is a failure, call rollback()
instead. 如果失败,请调用
rollback()
。
Nowadays, it's more usual to have a persistence mechanism like JPA manage transactions, working with the framework or container. 如今,使用JPA这样的持久性机制来管理事务,与框架或容器一起工作已变得越来越普遍。 This infrastructure can handle common transactional scenarios quite well.
该基础结构可以很好地处理常见的事务方案。
I would suggest that you not do this in a servlet. 我建议您不要在servlet中执行此操作。 Move the code into a persistence tier that you can test off-line, without the servlet container.
将代码移到可以离线测试的持久层,而无需servlet容器。
I'd also recommend Spring and JPA . 我还建议使用Spring和JPA 。 Layering your app so the web and persistence tiers aren't one will be a good idea.
对您的应用程序进行分层,使Web和持久性层不再是一个好主意。
http://java.sun.com/developer/technicalArticles/J2EE/jpa/ http://www.springsource.org/ http://java.sun.com/developer/technicalArticles/J2EE/jpa/ http://www.springsource.org/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.