所涉及的technology:
java, socket,hibernate,oracle
问题:
当我用浏览器向我侦听的端口发送数据时总是报如下错误:
Could not synchronize database state with session
ORA-01461: can bind a LONG value only for insert into a LONG column
背景,分析:
写一个SocketServer, 能够侦听所有发到这个端口上面的数据。
//以下是一个包装的方法,把从socket中得到的输入流,转换成字符串。 public static String streamToStringByByte(InputStream is) throws IOException{ //设置缓冲区长度 byte[] b = new byte[4096]; StringBuffer sb = new StringBuffer(); String tempStr = ""; while(is.read(b) != -1){ //设置解码字符集 //下面这句话打印出来的数据长度看似不长,但是细心观察就会发现,这个刚转换出来的字符串后面有很多很多的空格,应该就是上面的那个byte[]没有用到的地方,全部转换成了空格,所以导致出现了如题的错误。为了防止此类错误,应该加上用trim()去掉两边儿的空格。 tempStr = new String(b,Constant.ENCODING_CHARSET); sb.append(tempStr==null?"":tempStr); //查看当前的输入流是否已经结束 if(tempStr.endsWith(Constant.BEGIN_SYMBOL)){ //Utility.print("End of input stream....."); break; } } hibernateTemplate.save(new Message(sb.toString)); return sb.toString(); } |
原因:
是由于要保存的数据长度超过了数据库设定的长度。