Hello,
I'm using hibernate V2.1.3 in a Tomcat V4.1.30 servlet container. I made a servlet for providing images from a database. I tried to test my servlet in a simple html page. The html page contains some references to the servlet. At first when the html page was displayed was no problem, the images were visible on the display.
I pushed the refresh button in the browser sometimes very quickly. In a little while the servlet didn't answered and the browser was in waiting state.
I think that i'm using hibernate badly. Please help me, that where is the mistake in my code.
Here is the session provider class
Code:
public class HibernateSession {
private static SessionFactory sessionFactory;
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
if (s == null) {
// Don't get from JNDI, use a static SessionFactory
if (sessionFactory == null) {
// Use default hibernate.cfg.xml
sessionFactory = new Configuration().configure()
.buildSessionFactory();
}
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null) {
s.close();
}
}
}
Here is the Image provider class
Code:
public class ImageUtils {
private static Logger log = Logger.getLogger(ImageUtils.class);
public static Image getImage(Integer image_id) {
Session sess = null;
Image image = null;
Transaction tx = null;
try {
sess = HibernateSession.currentSession();
tx = sess.beginTransaction();
image = (Image)sess.load(Image.class, image_id);
tx.commit();
} catch (HibernateException he) {
log.error(he);
try {
if (tx != null) tx.rollback();
} catch (Exception e) {
log.error(e);
}
} finally {
try {
if (sess != null)
HibernateSession.closeSession();
} catch (Exception e) {
log.error(e);
}
}
return image;
}
}
Here is the servlet class:
Code:
public class ImgDsplayer2 extends HttpServlet {
private static Logger log = Logger.getLogger(ImgDsplayer2.class);
private static final String image_id_paramname = "image.id.name";
public void service(HttpServletRequest request, HttpServletResponse response) {
try {
String image_id_attrname = getInitParameter(image_id_paramname);
if (image_id_attrname != null) {
log.info("Image ID attribute name: " + image_id_attrname);
String image_id_str = request.getParameter(image_id_attrname);
log.info(image_id_attrname + " value: " + image_id_str);
if (image_id_str != null) {
Integer image_id = Integer.valueOf(image_id_str);
if (image_id != null) {
Image image = ImageUtils.getImage(image_id);
if (image != null) {
response.setContentType("image/jpeg");
OutputStream os = response.getOutputStream();
Blob b = image.getImgbinarydata();
os.write(b.getBytes(1, (int) b.length()));
os.flush();
os.close();
} else
log.warn("Image not found! IMAGE_ID: " + image_id);
} else
log.warn(image_id_attrname
+ " attribute value not found!");
} else
log.warn(image_id_attrname + " attribute not found!");
} else
log.warn("Servlet init parameter \"" + image_id_paramname
+ "\" not found!");
} catch (Exception e) {
log.error(e);
}
}
}
Here is the hibernate configuration file
Code:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.datasource">java:comp/env/jdbc/dbcucc</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="show_sql">false</property>
<property name="use_outer_join">true</property>
<property name="hibernate.dialect">net.sf.hibernate.dialect.Oracle9Dialect</property>
<property name="hibernate.default_schema">mohu</property>
<!-- Mapping files -->
<mapping resource="mo/hdb/schema/table/Image.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Here is the snippet from the tomcat server.xml
Code:
<Resource name="jdbc/dbcucc" scope="Shareable" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/dbcucc">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>validationQuery</name>
<value>select * from cms</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@database.host:1521:db</value>
</parameter>
<parameter>
<name>password</name>
<value>db</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>3000</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>username</name>
<value>db</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
</ResourceParams>
</Context>