import java.io.Serializable; import java.sql.Connection; import java.util.Hashtable; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; import javax.servlet.http.HttpServlet; import net.sf.hibernate.HibernateException; import net.sf.hibernate.Session; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.cfg.Configuration; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; public class TestServlet extends HttpServlet { public static String hello = "HELLO 1 - Dont forget to setup datasource 'convergent'"; public void init() { System.out.println("\n\n"+hello+"\n\n"); String cfgFolder = System.getProperty("CONVERGENT_CFG_HOME"); if(cfgFolder == null){ System.out.println("\n\nPlease set -DCONVERGENT_CFG_HOME=whereverYouPut'coLog4j.xml'WithoutEnding'/' at server start"); return; } try{ // Init & test log4j String log4jCfgFile = cfgFolder + "/coLog4j.xml"; System.out.println("\n\nlog4jCfgFile="+log4jCfgFile); DOMConfigurator.configure(log4jCfgFile); Logger log = Logger.getLogger(TestServlet.class.getName()); log.info("Logging initialized."); System.out.println("Logging initialized - println() generated message"); // Init & test hibernate String jndiContextFactoryClass = "org.jnp.interfaces.NamingContextFactory"; String jndiProviderUrl = "localhost"; String hibernateConnectionDatasource = "java:/convergent"; String hibernateDialect = "net.sf.hibernate.dialect.MySQLDialect"; String hibernateConnectionProviderClass= "net.sf.hibernate.connection.DatasourceConnectionProvider"; String hibernateStatementCacheSize = "0"; String hibernateCacheProviderClass = "net.sf.hibernate.cache.HashtableCacheProvider"; String hibernateMaxFetchDepth = "1"; String transactionFactoryClass = "net.sf.hibernate.transaction.JDBCTransactionFactory"; String showSql = "true"; Properties props = new Properties(); props.put("hibernate.connection.datasource", hibernateConnectionDatasource); props.put("hibernate.dialect", hibernateDialect); props.put("hibernate.connection.provider_class", hibernateConnectionProviderClass); props.put("hibernate.statement_cache.size", hibernateStatementCacheSize); props.put("hibernate.cache.provider_class", hibernateCacheProviderClass); props.put("hibernate.max_fetch_depth", hibernateMaxFetchDepth); props.put("transaction.factory_class", transactionFactoryClass); props.put("show_sql", showSql); Configuration hbCfg = new Configuration(); hbCfg.setProperties(props); hbCfg.addClass(CoConfig.class); SessionFactory sf = hbCfg.buildSessionFactory(); log.debug("Hibernate configured & session factory built"); Hashtable parms = new Hashtable(); parms.put(Context.INITIAL_CONTEXT_FACTORY, jndiContextFactoryClass); parms.put(Context.PROVIDER_URL, jndiProviderUrl); InitialContext ic = new InitialContext(parms); DataSource ds = (DataSource)ic.lookup(hibernateConnectionDatasource); Connection c = ds.getConnection(); log.debug("Connection obtained."); Session s = sf.openSession(c); CoConfig cc = (CoConfig) s.load(CoConfig.class, new Long(1L)); // row 1, pk=1 s.close().close(); log.debug("Retrieved row, code="+cc.getCode()); }catch(Exception e){ e.printStackTrace(); } } }