Dear colleagues,
Please help us to found a solution to our problems.
Could we save a files on HDD if we use a EJB - conception?
-
The problem of transfer and store file from EJB (4 messages)
- Posted by: Alexandr Reshetnikov
- Posted on: February 13 2003 08:04 EST
Threaded Messages (4)
- Handling File using EJB by shervin Sadeghi on February 13 2003 20:01 EST
- Reply by Alex Pisarev on February 14 2003 03:12 EST
-
Reply by Alexandr Reshetnikov on February 14 2003 09:21 EST
- Alexander by Alex Pisarev on February 18 2003 03:45 EST
-
Reply by Alexandr Reshetnikov on February 14 2003 09:21 EST
- Reply by Alex Pisarev on February 14 2003 03:12 EST
-
Handling File using EJB[ Go to top ]
- Posted by: shervin Sadeghi
- Posted on: February 13 2003 20:01 EST
- in response to Alexandr Reshetnikov
I solved the problem using Statless session bean which use the DAO like this:
public class FileHandlerDAOImpl implements FileHandlerDAO {
private Connection dbConnection;
final static String THIS="FileHandlerDAOImpl";
public FileDTO reterieveFile(int fileID)
throws FileHandlerDAOSysException,FileNotFoundException,
FileReterieveException{
PreparedStatement pstmt = null;
ResultSet result = null;
FileDTO fileDTO=null;
try {
getConnection();
pstmt=dbConnection.prepareStatem(pqueryStr);
pstmt.setInt(1,fileID);
result = pstmt.executeQuery();
int i=1;
if (result!=null && result.next()){
fileDTO=new FileDTO();
fileDTO.setFileID(result.getInt(i++));
fileDTO.setFileName(result.getString(i++));
fileDTO.setFormat(result.getString(i++));
int length=result.getInt(i++);
fileDTO.setSize(length);
InputStream stream=result.getBinaryStream(i++);
byte[] buffer = new byte[length];
int bytesRead=0;
if((bytesRead = stream.read(buffer, 0, length)) != -1){
fileDTO.setContent(buffer);
}else{
Debug.println("Cannot reterive file size"+length);
throw new FileReterieveException(THIS+ "Unable to retrieve file Content"
+"with this size= "+length);
}
Debug.println(THIS+ "File was reterieved:");
}else{
Debug.println("FileNot Found Exception");
throw new FileNotFoundException("Couldnt find such file in database");
}
} catch(SQLException se) {
Debug.println("Exception"+se.getMessage());
throw new FileHandlerDAOSysException(THIS+ "Unable to Query for File"
+ se.getMessage());
} catch(Exception ex) {
ex.printStackTrace();
Debug.println("Exception"+ex.getMessage());
throw new FileReterieveException(THIS+ "Unable to retrieve file Content"
+ ex.getMessage());
} finally {
closeResultSet(result);
closeStatement(pstmt);
closeConnection();
}
return fileDTO;
}
public int storeFile(FileDTO fileDTO)
throws FileStoreException,FileHandlerDAOSysException{
PreparedStatement pstmt = null;
PreparedStatement pstmt1 = null;
ResultSet result=null;
String pqueryStr="INSERT INTO "+DatabaseNames.FileHandler_TABLE+"(file_name,format,size,content) "
+" values(?,?,?,?)";
String pqueryStr1="SELECT @@IDENTITY FROM FileHandler";
Debug.println(THIS+ "Query:"+ pqueryStr);
try {
int i=1;
getConnection();
pstmt=dbConnection.prepareStatement(pqueryStr);
pstmt1=dbConnection.prepareStatement(pqueryStr1);
Debug.println(THIS+ "Step0");
pstmt.setString(i++,fileDTO.getFileName());
Debug.println(THIS+ "Step1"+fileDTO.getFileName());
pstmt.setString(i++,fileDTO.getFormat());
Debug.println(THIS+ "Step2"+fileDTO.getFormat());
pstmt.setInt(i++,fileDTO.getContent().length);
ByteArrayInputStream bais=new ByteArrayInputStream(fileDTO.getContent());
pstmt.setBinaryStream(i++,bais,fileDTO.getContent().length);
int resultCount = pstmt.executeUpdate();
Debug.println(THIS+"queryInsert was executed and the resultCount is "+resultCount);
if ( resultCount != 1 )
throw new FileStoreException("Couldnt insert data in database");
result=pstmt1.executeQuery();
int pKey=0;
if(!result.next())
throw new FileStoreException(
"Couldnt find Last UpdateKey in database");
else
pKey=result.getInt(1);
Debug.println("pKey for this insert is"+pKey);
return pKey;
} catch(SQLException se) {
se.printStackTrace();
Debug.println("SQLException"+se.getMessage());
throw new FileHandlerDAOSysException(THIS+ "Unable to insert for File"
+ se.getMessage());
} catch(Exception ex) {
ex.printStackTrace();
Debug.println("Exception"+ex.getMessage());
throw new FileStoreException(THIS+ "Unable to store file Content"
+ ex.getMessage());
} finally {
closeStatement(pstmt);
closeConnection();
}
}
}
I hope this help:-) -
Reply[ Go to top ]
- Posted by: Alex Pisarev
- Posted on: February 14 2003 03:12 EST
- in response to shervin Sadeghi
Sherdin,
I think Alexander meant storing the file directly on the dist rather than putting it into the database.
Alexander,
I recommend you to develop a JCA adapter for manipulations with files manipulations and then use it from your EJB. It will be safe enough and will not violate the spec.
Another option is just to write the file to disk and ignore the spec recommendations - anyway, it's just recommendation, isn't it? However, you should be very careful with multiuser file locking aspects in that case. Remember, that container will not prevent you from doing that, but I wouldn't do that in a production-level application if I were you.
Some containers offer non-J2EE-compatible file services. For example, Weblogic has T3 File Service for that purposes
Hope this helps.
Alex -
Reply[ Go to top ]
- Posted by: Alexandr Reshetnikov
- Posted on: February 14 2003 09:21 EST
- in response to Alex Pisarev
Dear Alex,
You are from Russia, aren't You? If it's so, could You let me know of Your e-mail address?
If possible, I would like to tell You about the problem in native language -
Alexander[ Go to top ]
- Posted by: Alex Pisarev
- Posted on: February 18 2003 03:45 EST
- in response to Alexandr Reshetnikov
You can use rogerwilco at rambler dot ru