Export to GitHub

full-hibernate-plugin-for-struts2 - issue #11

java.lang.IllegalStateException thrown for struts2 <result type="stream">


Posted on Jan 14, 2010 by Massive Kangaroo

What steps will reproduce the problem? 1. in struts.xml file let your package extend "hibernate-default" 2. add an action tag with an action class mapped. 3. add a result tag with type set to "stream"

What is the expected output? What do you see instead? Expected: Once a request for this action comes in the file is streamed back as response with out any exceptions thrown. Actual: File streams correctly, but finally throws an exception on the server console. java.lang.IllegalStateException. I believe the hibernate plug-in is trying to write something to an already committed output stream causing the exception.

What version of the product are you using? On what operating system? struts2-fullhibernatecore-plugin-2.1.2-GA struts-2.1.8.1 OS: Windows Vista SP2 Java: 1.6.0_16

Please provide any additional information below. Easily replicable using the struts provided examples (showcase) for file download. Just change the extends attribute of the package tag from "struts-default" to "hibernate-default".

Attachments

Comment #1

Posted on Jan 15, 2010 by Grumpy Rabbit

Thanks for this report. But, could you send me the Action code and the XML mappging file?

Comment #2

Posted on Jan 15, 2010 by Grumpy Rabbit

I made a simple example using Convention Plugin and I had No problem.

Model class:

@Entity public class ImageTest implements Serializable{

@Id
@GeneratedValue(strategy = IDENTITY)
int idimage;

@Column
byte[] content;

public ImageTest() {
    // TODO Auto-generated constructor stub
}

public ImageTest(int idimage, byte[] content) {
    super();
    this.idimage = idimage;
    this.content = content;
}

public int getIdimage() {
    return idimage;
}

public void setIdimage(int idimage) {
    this.idimage = idimage;
}

public byte[] getContent() {
    return content;
}

public void setContent(byte[] content) {
    this.content = content;
}

}

Action:

@ParentPackage("hibernate-default") public class VerImagemAction extends ActionSupport{

@SessionTarget
Session session;

InputStream inputStream;

@Action(
    results=@Result(type="stream")
)
public String execute() throws Exception {
    ImageTest imagem = (ImageTest) session.get(ImageTest.class, 1);
    inputStream = new ByteArrayInputStream(imagem.getContent());
    return super.execute();
}

public InputStream getInputStream() {
    return inputStream;
}

}

hibernate.cfg.xml

com.mysql.jdbc.Driver admin jdbc:mysql://localhost:3306/ufpa root org.hibernate.dialect.MySQLDialect update

No struts.xml

The Stream was successull generated and downloaded with no exception.

Hibernate Core 3.3.2 Hibernate Annotations 3.4 Hibernate Validator 3.1 MySql 5.1 Struts 2.1.8 FHP 2.1.2

Comment #3

Posted on Jan 15, 2010 by Massive Kangaroo

Hi jyoshiriro, I've tried to detail it as much as possible, if you need something please do let me know. Thanks.

Struts.xml :

application/pdf inputStream filename="document.pdf" 1024

Model/Entity, DocumentTempImpl :

public class DocumentTempImpl {

private Long objectId;
private String documentCode;
private byte[] data;

public DocumentTempImpl() {
    super();
}

public DocumentTempImpl(Long objectId) {
    super(objectId);
}

public Long getObjectId() {
    return objectId;
}

public void setObjectId(Long objectId) {
    this.objectId = objectId;
}
public String getDocumentCode() {
    return documentCode;
}

public void setDocumentCode(String documentCode) {
    this.documentCode = documentCode;
}

public byte[] getData() {
    return data;
}

public void setData(byte[] data) {
    this.data = data;
}

}

Action, DownloadFileAction.java :

import xyz.Document; import xyz.DocumentTempImpl; import java.io.ByteArrayInputStream; import java.io.InputStream;

import com.opensymphony.xwork2.ActionSupport;

/* * Attempts to fetch a pdf file and send to user. * The pdf file resides in DB, Fetched using hibernate. *
*/ public class DownloadFileAction extends ActionSupport {

private String documentId = "98";
private InputStream inputStream;

@SessionTarget
Session hSession;

@TransactionTarget
Transaction hTransaction;

public String execute() throws Exception {
    fetch();
    return SUCCESS;
}

private void fetch() throws Exception {
    DocumentTempImpl doc = (DocumentTempImpl)find(DocumentTempImpl.class,
            Long.valueOf(documentId));

    byte[] data = doc.getData();
    inputStream = new ByteArrayInputStream(data);

}

public InputStream getInputStream() {
    return inputStream;
}

public Object find(Class clazz, Long id) {
    Query q = hSession.createQuery("from " + clazz.getName()
            + " as t where t.id =:longVal");
    q.setLong("longVal", id);

    return q.uniqueResult();
}

}

Hibernate Configuration, hibernate.cfg.xml :

true UTF-8 com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/test test test org.hibernate.dialect.MySQLDialect thread org.hibernate.transaction.JDBCTransactionFactory

    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="use_sql_comments">true</property>

    <mapping resource="xyz/DocumentTempImpl.hbm.xml" />

</session-factory>

Hibernate Mapping, DocumentTempImpl.hbm.xml :

<class name="xyz.DocumentTempImpl" table="DOCUMENT_TEMP">
    <id name="objectId" column="DOCUMENT_TEMP_ID" access="field">
        <generator class="native" />
    </id>
    <property name="documentCode" column="CODE" type="string"
        access="field" />
    <property name="data" access="field">
        <column name="FILE" sql-type="MEDIUMBLOB" />
    </property>
</class>

Hibernate Core 3.3.2 GA Hibernate Annotations 3.4 Hibernate Validator 3.1 MySql 5.1.41 Struts 2.1.8 FHP 2.1.2 GA JRE/JDK 1.6

Attachments

Comment #4

Posted on Jan 15, 2010 by Massive Kangaroo

If this is useful.... apache-tomcat-5.5.28

Comment #5

Posted on Jan 15, 2010 by Grumpy Rabbit

I used your file in a new project using Apache Derby Embedded Database and the data was downloaded successful.

Is the content of you colunm "data" a valid PDF file?

Comment #6

Posted on Jan 15, 2010 by Grumpy Rabbit

I tried now with mysql 5.1 with success too. But I use Tomcat 6.x.

So, the problem can be the Tomcat version or the content of your table.

Comment #7

Posted on Jan 16, 2010 by Massive Kangaroo

Yes it was tomcat-5.5.28, that was causing the problem. I changed to tomcat-6.0.20 and the exception stopped showing up.

Thanks a lot.

Status: Invalid

Labels:
Type-Defect Priority-Medium