存储用户上传图像的 Hibernate+Spring+MySQL 项目操作的性能调优
我正在开发一个基于Spring+Hibernate+MySQL的Web项目。我陷入了必须将用户上传的图像存储到数据库的困境。虽然我已经编写了一些目前运行良好的代码,但我相信当项目上线时事情会变得一团糟。
这是我的域类,它携带图像字节:
@Entity
public class Picture implements java.io.Serializable{
long id;
byte[] data;
... // getters and setters
}
这是我的控制器,用于在提交时保存文件:
public class PictureUploadFormController extends AbstractBaseFormController{
...
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception{
MutlipartFile file;
// getting MultipartFile from the command object
...
// beginning hibernate transaction
...
Picture p=new Picture();
p.setData(file.getBytes());
pictureDAO.makePersistent(p); // this method simply calls getSession().saveOrUpdate(p)
// committing hiernate transaction
...
}
...
}
显然是一段糟糕的代码。无论如何,我可以使用 InputStream 或 Blob 来保存数据,而不是首先将用户的所有字节加载到内存中,然后将它们推入数据库中?
我对 Hibernate 对 Blob 的支持做了一些研究,并在《Hibernate In Action》一书中找到了这一点:
java.sql.Blob 和 java.sql.Clob 是 处理大件最有效的方法 Java 中的对象。不幸的是,一个 Blob 或 Clob 的实例只是 直到 JDBC 事务为止都可用 完成。 所以如果你的持久类 定义 java.sql.Clob 的属性或 java.sql.Blob(不是一个好主意 无论如何),你将受到限制 可以使用该类的实例。 特别是,你将无法使用 该类的实例是分离的 对象。此外,许多 JDBC 驱动程序不提供工作支持 对于 java.sql.Blob 和 java.sql.Clob。 因此,绘制地图更有意义 使用二进制或文本的大对象 映射类型,假设检索 将整个大对象放入内存 不是性能杀手。
注意你 可以找到最新的设计模式 以及关于大对象使用的提示 Hibernate 网站,有技巧 特定平台。
现在显然不能使用Blob,因为无论如何都不是一个好主意,还有什么可以用来提高性能呢?我在 Hibernate 网站上找不到任何最新的设计模式或任何有用的信息。因此,来自 stackoverflowers 的任何帮助/建议将不胜感激。
谢谢
I am working on a web project that is Spring+Hibernate+MySQL based. I am stuck at a point where I have to store images uploaded by a user into the database. Although I have written some code that works well for now, but I believe that things will mess up when the project would go live.
Here's my domain class that carries the image bytes:
@Entity
public class Picture implements java.io.Serializable{
long id;
byte[] data;
... // getters and setters
}
And here's my controller that saves the file on submit:
public class PictureUploadFormController extends AbstractBaseFormController{
...
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception{
MutlipartFile file;
// getting MultipartFile from the command object
...
// beginning hibernate transaction
...
Picture p=new Picture();
p.setData(file.getBytes());
pictureDAO.makePersistent(p); // this method simply calls getSession().saveOrUpdate(p)
// committing hiernate transaction
...
}
...
}
Obviously a bad piece of code. Is there anyway I could use InputStream or Blob to save the data, instead of first loading all the bytes from the user into the memory and then pushing them into the database?
I did some research on hibernate's support for Blob, and found this in Hibernate In Action book:
java.sql.Blob and java.sql.Clob are
the most efficient way to handle large
objects in Java. Unfortunately, an
instance of Blob or Clob is only
useable until the JDBC transaction
completes. So if your persistent class
defines a property of java.sql.Clob or
java.sql.Blob (not a good idea
anyway), you’ll be restricted in how
instances of the class may be used. In
particular, you won’t be able to use
instances of that class as detached
objects. Furthermore, many JDBC
drivers don’t feature working support
for java.sql.Blob and java.sql.Clob.
Therefore, it makes more sense to map
large objects using the binary or text
mapping type, assuming retrieval of
the entire large object into memory
isn’t a performance killer.Note you
can find up-to-date design patterns
and tips for large object usage on the
Hibernate website, with tricks for
particular platforms.
Now apparently the Blob cannot be used, as it is not a good idea anyway, what else could be used to improve the performance? I couldn't find any up-to-date design pattern or any useful information on Hibernate website. So any help/recommendations from stackoverflowers will be much appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该书的修订版(Java Persistence with Hibernate)说:
希望这有帮助。
The revised edition of the book (Java Persistence with Hibernate) says:
Hope this helps.