为了账号安全,请及时绑定邮箱和手机立即绑定

关于IO的问题,为什么结束了将持久化对象保存到数据库最后关掉了inputstream会导致错误。

public void testSavePicture() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date;
		try {
			date = sdf.parse("1987-04-01");
			Student s = new Student(0, "kula", date);
			File file = new File("/Users/kk/Desktop/photo/3.pic.jpg");			
			InputStream in = new FileInputStream(file);
			Blob image = Hibernate.getLobCreator(session).createBlob(in, in.available());
			s.setPicture(image);
			session.save(s);
			
			//in.close(); 如果关掉输入流会报错。
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

如果关闭了输入流执行不成功会报以下错误。

avax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute statement

    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)

    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)

    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)

    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1402)

    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:472)

    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3132)

    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2369)

    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:467)

    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)

    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)

    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:221)

    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)

    at junit.StudentTest.destroy(StudentTest.java:120)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

    at java.lang.reflect.Method.invoke(Method.java:498)

    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)

    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)

    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)

    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)

    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)

    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)

    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)

    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)

    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)

    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)

    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)

    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)

    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by: org.hibernate.exception.GenericJDBCException: could not execute statement

    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)

    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)

    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)

    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)

    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)

    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2934)

    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3434)

    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)

    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:560)

    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434)

    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)

    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)

    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1396)

    ... 32 more

Caused by: java.sql.SQLException: Error reading from InputStream java.io.IOException

    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)

    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)

    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)

    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)

    at com.mysql.jdbc.PreparedStatement.readblock(PreparedStatement.java:2664)

    at com.mysql.jdbc.PreparedStatement.streamToBytes(PreparedStatement.java:4456)

    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2190)

    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2052)

    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)

    at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094)

    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)

    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)

    ... 41 more

Caused by: java.io.IOException: Stream Closed

    at java.io.FileInputStream.readBytes(Native Method)

    at java.io.FileInputStream.read(FileInputStream.java:255)

    at com.mysql.jdbc.PreparedStatement.readblock(PreparedStatement.java:2662)

    ... 48 more

可是io不是应该在用完后就关掉吗。

正在回答

2 回答

这个没法解释取出照片关闭了io,能正常通过。

public void testGetPicture() {
		Student s = (Student) session.get(Student.class, 1);
		Blob image = s.getPicture();
		try {
			FileOutputStream out = new FileOutputStream("image/c1py.jpg");
			InputStream in = image.getBinaryStream();
			byte[] barray = new byte[1024];
			int bytes;
			while ((bytes = in.read(barray, 0, barray.length)) > 0) {
				out.write(barray, 0, bytes);
			}
			out.close();
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

以上代码可以执行。

0 回复 有任何疑惑可以回复我~
#1

最终也没有

正如一楼的朋友说的。 session.save后,还要运行transaction.commit(); // 提交事务,才能把数据写入数据库中 你试试把:transaction.commit(); 写在session.save的下一行 然后再在下一行写入in.close();就不会报错了。 而你展示的代码,是读取的时候的操作。 没有写入操作,所以不需要向数据库提交事务。 不提交事务,所以没有用到transaction.commit();这个代码 所以可以直接使用in.close()
2016-08-02 回复 有任何疑惑可以回复我~

session保存对象并不是调用session.save之后立即写入到数据库中,而是等待提交事务的时候才会写入到数据库中

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

关于IO的问题,为什么结束了将持久化对象保存到数据库最后关掉了inputstream会导致错误。

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信