ps = conn.prepareStatement(str2)前,ps是否一定要close??????
public int create(AttachmentValue detail) throws InfoDAOException
{
Connection conn = null;
PreparedStatement ps = null;
int rownum = 0;
String str = "insert into attachment (id, infoid, attachment, ext, " +
"filename, attachmentsize, description) values (seq_attachment.nextval, ?, ?, ?, ?, ?, ?)";
String str2 = "select seq_attachment.currval from dual";
String str3 = "select attachment from attachment where id = ? for update";
try
{
//conn = getConnection();
conn = getWriteConnection();
try
{
conn.setAutoCommit(false);
}
catch (Exception e)
{
}
boolean attachment_flag = true;
ps = conn.prepareStatement(str);
ps.setInt(1, detail.getInfoid());
if (detail.getAttachment() != null)
{
//ps.setBytes(2, detail.getAttachment());
//ByteArrayInputStream stream = new ByteArrayInputStream (detail.getAttachment());
//ps.setBinaryStream(2,stream,detail.getAttachment().length);
ps.setBytes(2, " ".getBytes());
}
else
{
ps.setBytes(2, " ".getBytes());
attachment_flag = false;
}
if (detail.getExt() != null)
{
ps.setString(3, detail.getExt());
}
else
{
ps.setNull(3, java.sql.Types.VARCHAR);
}
ps.setString(4, detail.getFilename());
if (attachment_flag)
{
ps.setInt(5, detail.getAttachment().length);
}
else
{
ps.setInt(5, 0);
}
if (detail.getDescription() != null)
{
ps.setString(6, detail.getDescription());
}
else
{
ps.setNull(6, java.sql.Types.VARCHAR);
}
rownum = ps.executeUpdate();
//**************************************************
ps.close(); //是否必须????
//**************************************************
ps = conn.prepareStatement(str2);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
rownum = rs.getInt(1);
}
//**************************************************
ps.close() ; //是否必须????
//**************************************************
rs.close();
if ( attachment_flag )
{
ps = conn.prepareStatement(str3);
ps.setInt(1, rownum);
rs = ps.executeQuery();
if( rs.next())
{
java.io.OutputStream os = null;
if (appserver == 0)
{
java.sql.Blob myBlob = null;
myBlob = rs.getBlob(1);
os = ((weblogic.jdbc.common.OracleBlob) myBlob).getBinaryOutputStream();
}
else if (appserver >= 1)
{
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob(1);
os = blob.getBinaryOutputStream();
}
os.write(detail.getAttachment());
os.flush();
os.close();
}
//**************************************************
ps.close() ; //是否必须????
//**************************************************
rs.close();
}
ps.close();
try
{
conn.commit();
}
catch (Exception e)
{
}
detail.setId(rownum);
if( attachment_flag )
{
detail.setAttachmentsize(detail.getAttachment().length);
}
else
{
detail.setAttachmentsize(0);
}
}
catch (Exception e)
{
try
{
conn.rollback();
}
catch(Exception ex)
{
}
//e.printStackTrace();
throw new InfoDAOException("劤藤Attachment斤口珂??! " + e.toString());
}
finally
{
ps = null;
try
{
conn.setAutoCommit(true);
}
catch(Exception e)
{
}
try
{
conn.close();
}
catch (Exception e)
{
}
conn = null;
}
return rownum;
}