所用框架ssm,数据库是mysql
最近在学习springmvc的时候,做文件下载的时候遇到了Connection is read-only. Queries leading to data modification are not allowed
网上查找原因发现是因为自己在spring配置文件中增加了事务管理,,这里我给业务层com.study.service.impl包下所有的类和方法添加了事物,除了已insert* 、delete*和update*方法开头的方法事务不是只读的其他都是只读的,
applicationContext.xml详细配置如下
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd" default-autowire="byName" >
我的文件下载流程,首先进入一个页面,查询数据库中所有的文件信息,点击下载,去服务器上web应用中下载文件,并更新文件次数
测试页面如下
点击下载按钮,去下载项目中的文件,并更新数据库中的下载次数
controller控制器类如下,调用download控制器:
package com.study.controller;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.study.pojo.Files;
import com.study.service.FilesService;
@Controller
public class FilesDownloadController {
@Resource
private FilesService filesServiceImpl;
@RequestMapping("getAll")
public String getAllFileInfo(Model model){
List
model.addAttribute("list", list);
return "main";
}
@RequestMapping("download")
public String downLoadFile(int id,String filename,HttpServletResponse rep,HttpServletRequest req) throws IOException{
filesServiceImpl.updateFilesCount(id);
rep.setHeader("Content-Disposition", "attchment;filename="+filename);
String path = req.getServletContext().getContextPath();
File file = new File(path, filename);
PrintWriter out = rep.getWriter();
return null;
}
}
业务逻辑层代码如下:
public class FilesServiceImpl implements FilesService {
@Resource
private FilesMapper filesMapper;
public List
return filesMapper.selAll();
}
public int updateFilesCount(int id) {
// TODO Auto-generated method stub
return filesMapper.updateFile(id);
}
}
注意我这里的update方法与事务中的不一致,配置文件中,事务配置可能手欠,写成了
spring配置文件中的事务配置修改成