jdbctemp 事务

<%@ page import="java.net.URLEncoder"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/WEB-INF/tlds/wall.tld" prefix="wall"%>
<%@ taglib prefix="c" uri="/WEB-INF/tlds/c.tld"%>
<%
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.flushBuffer();
%>
<%@page import="org.springframework.jdbc.core.PreparedStatementCallback"%><html>
<head>
<title></title>
</head>
<body>

<%
javax.sql.DataSource dataSource = (javax.sql.DataSource)WebApplicationContextUtils.getWebApplicationContext(
pageContext.getServletContext()).getBean("luxe_dataSource");

org.springframework.jdbc.core.JdbcTemplate jdbcTemp= new org.springframework.jdbc.core.JdbcTemplate(dataSource);


// System.out.println(" ==== dataSource : " + dataSource);
// System.out.println(" ==== jdbcTemplare : " + jdbcTemp);




String sql = "select id,name,order_index from luxe_poi_cat where book_id=? and parent_id=0 and is_delete=0 order by order_index;";
String childrenSql = "select id,name,order_index from luxe_poi_cat where book_id=? and parent_id=? and is_delete=0 order by order_index;";
String updateOrderSql ="update luxe_poi_cat set order_index=? where id=?";

String sBookId=request.getParameter("book_id");

int iBookId =0;
if(sBookId!=null && sBookId.trim().length()>0){
try{
iBookId = Integer.parseInt(sBookId);
System.out.println(" book_id==== ["+sBookId+"]");
out.println(" book_id==== ["+sBookId+"]");
}catch(NumberFormatException ne){
System.out.println(" ==== paramter [book_id] just accept integer type ");
out.println(" ==== paramter [book_id] just accept integer type ");
ne.printStackTrace();
System.exit(1);
}
}else{
System.out.println(" ==== paramter [book_id] must not be empty ");
out.println(" ==== paramter [book_id] must not be empty ");

}

[code="java"] org.springframework.transaction.support.DefaultTransactionDefinition def = new org.springframework.transaction.support.DefaultTransactionDefinition();
org.springframework.transaction.PlatformTransactionManager transactionManager =
(org.springframework.transaction.PlatformTransactionManager)
WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext()).getBean("transactionManager");
org.springframework.transaction.TransactionStatus status = transactionManager.getTransaction(def);

try{
List list=jdbcTemp.queryForList(sql,new Object[]{iBookId});
// System.out.println(" >>> list : " + list);
if(list!=null && list.size()>1){
for(int i=0;i< list.size();i++){
Map recordMap=(Map)list.get(i);
System.out.println(" ================= Record ===== " + recordMap);
int pkKey= Integer.parseInt(recordMap.get("id").toString());

//update chirldren order with parent id
List childrenList=jdbcTemp.queryForList(childrenSql,new Object[]{iBookId,pkKey});

if(childrenList!=null){
for(int j=0;j< childrenList.size();j++){
Map childrenRecordMap=(Map)list.get(j);

System.out.println(" =================children Record ===== " + childrenRecordMap);
int childrenPKKey= Integer.parseInt(childrenRecordMap.get("id").toString());
jdbcTemp.update(updateOrderSql,new Object[]{j+2,childrenPKKey});
}
}
//update parent order id.
jdbcTemp.update(updateOrderSql,new Object[]{i+2,pkKey});

}
			//	transactionManager.commit(status);

}else{
System.out.println(" book_id==== ["+sBookId+"] had not entry!");
out.println(" book_id==== ["+sBookId+"] had not entry! ");

}
}catch(org.springframework.dao.DataAccessException ex){
[quote]		//   transactionManager.rollback(status);[/quote]

ex.printStackTrace();
System.out.println("== SQL rolback ==");
out.println("<br/> == SQL rolback ==<br/>");
}

System.out.println("== End ==");
out.println("<br/> == End ==<br/>");

%>



</body>
</html>

事务没提交 导致 数据库其它操作(session)

无法 操作..

死活还是原来的数据


<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="luxe_dataSource"/>
</bean>

[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值