Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
The problem is occuring at doTestHibernateInsertIntoInstalledProduct method.
Hibernate version:
2.1.6
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="org.hibernate.test.Product" table="t$Product" lazy="false">
<id type="long" name="id" column="id$">
<generator class="native"/>
</id>
<property name="vendor" type="string" length="254">
<column name="vendor" index="idx_product_ven_nam_ver"/>
</property>
<property name="name" type="string" length="254">
<column name="name" index="idx_product_ven_nam_ver"/>
</property>
<property name="version" type="string" length="64">
<column name="version" index="idx_product_ven_nam_ver"/>
</property>
<property name="category" type="string" length="64"/>
<property name="helplink" type="string" length="254"/>
</class>
<class name="org.hibernate.test.UnitaryComputerSystem" table="t$UnitaryComputerSystem" lazy="false">
<id type="long" name="id" column="id$">
<generator class="native"/>
</id>
<property name="name" type="string" length="254">
<column name="name" index="idx_ucs_name"/>
</property>
</class>
<class name="org.hibernate.test.Directory" table="t$Directory" lazy="false">
<id type="long" name="id" column="id">
<generator class="native"/>
</id>
<property name="path" type="string" length="254">
<column name="path" index="idx_directory_path"/>
</property>
</class>
<class name="org.hibernate.test.InstalledProduct" table="t$InstalledProduct">
<id type="long" name="pinstanceid" column="id$">
<generator class="native"/>
</id>
<many-to-one name="ComputerSystem" class="org.hibernate.test.UnitaryComputerSystem" column="ComputerSystem" not-null="true" foreign-key="fk_unitarycomputersystem_id" index="idx_installedproduct_computersystem"/>
<many-to-one name="Product" class="org.hibernate.test.Product" column="Product" not-null="true" foreign-key="fk_product_id" index="idx_installedproduct_product" />
<many-to-one name="DirectoryId" class="org.hibernate.test.Directory" column="directoryid" not-null="true" foreign-key="fk_directory_id" index="idx_installedproduct_directoryid" />
<property name="ProductIdentifier" type="string" length="254"/>
<property name="InternalVersion" type="string" length="64"/>
<property name="Language" type="integer" index="idx_installedproduct_language"/>
<property name="UnInstallString" type="string" length="254"/>
<property name="InstallationSource" type="string" length="254"/>
<property name="FriendlyName" type="string" length="254"/>
<property name="SupportPack" type="string" length="254"/>
<property name="ProductEdition" type="string" length="254"/>
<property name="LastExecutionTime" type="long"/>
<property name="FrequencyOfUsage" type="integer"/>
<property name="Description" type="string" length="254"/>
<property name="SoftwareType" type="integer"/>
<property name="PackageGUID" type="string" length="64"/>
<property name="AdrMsiKey" type="string" length="128"/>
<property name="InformationSrc_Bitmap" type="integer"/>
<many-to-one name="FK_pinstanceid" class="org.hibernate.test.InstalledProduct" column="fk_pinstanceid" not-null="false" foreign-key="fk_installedproduct_pinstanceid" index="idx_installedproduct_fk_pinstanceid"/>
</class>
<class name="org.hibernate.test.Time" table="t$Time">
<id name="sno" type="long">
<column name="SNo"/>
<generator class="native"/>
</id>
<property name="Approach" type="string" length="100"/>
<property name="ResultSize" type="int"/>
<property name="Duration" type="long"/>
<property name="Action" type="string"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
package org.hibernate.test;
import java.io.*;
import java.util.*;
import java.lang.Integer;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import junit.framework.Assert;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.cfg.Environment;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
//Required for Pure JDBC related
import java.sql.*;
import com.novell.zenworks.desktop.inventory.storer.BatchPreparedStatement;
//Randomization
import java.util.Random;
public class InstalledProductTest extends junit.framework.TestCase //extends TestCase
{
public static int distinctUcs=0;
public static int productPerUcs=0;
public static int batchSize=0;
public static int COMMIT_COUNT=0;
StringBuffer sBuf=new StringBuffer();
public static SessionFactory sf=null;
public static Session s = null;
public static Connection conn = null;
public static Configuration cfg = null;
public static PreparedStatement pstmt_getId = null;
final int RAND_MAX_LIMIT=10000;
Random randomNumber=new Random();
Properties testProps = null;
static
{
try{
cfg = new Configuration()
.addFile("F:\\hibernate-2.1\\test\\org\\hibernate\\test\\Product.hbm.xml");
sf = cfg.buildSessionFactory();
}catch(Exception e)
{
}
}
public Session openSession() throws HibernateException {
if ( s == null )
{
s = sf.openSession();
conn = s.connection();
}
return s;
}
public void closeSession() throws HibernateException {
if (s != null)
{
s.close();
s = null;
}
}
public void setUp() throws Exception {
System.out.println("Calling SetUp");
}
public void cleanUp(String approach) throws Exception {
Statement del_stmt=conn.createStatement();
long op3=System.currentTimeMillis();
int numDeleted = del_stmt.executeUpdate("DELETE FROM T$INSTALLEDPRODUCT");
numDeleted += del_stmt.executeUpdate("DELETE FROM T$PRODUCT");
numDeleted += del_stmt.executeUpdate("DELETE FROM T$DIRECTORY");
numDeleted += del_stmt.executeUpdate("DELETE FROM T$UNITARYCOMPUTERSYSTEM");
conn.commit();
long op4=System.currentTimeMillis();
del_stmt.close();
recordTime(approach, "Delete", numDeleted, op4-op3);
}
public void runTest() throws Throwable {
super.runTest();
}
public static void main(String[] args) throws Exception
{
TestRunner.run( suite() );
}
public InstalledProductTest(String arg0)
{
super(arg0);
}
public static boolean batchUpdate(PreparedStatement psmnt) throws Exception
{
boolean bStatus=true;
try{
int [] ret=psmnt.executeBatch();
if(ret!=null)
{
for(int i=0;i<ret.length;i++)
{
if(ret[i]==-2 || ret[i]>0) continue;
else
{
bStatus=false;
throw new Exception("Batch Update failed at "+i+" value="+ret[i]);
}
}
//if(ret.length>0)System.out.println("Batch updated="+ret.length);
}
psmnt.clearBatch();
}
catch(BatchUpdateException be)
{
throw new Exception (be.getMessage());
}
catch(SQLException e)
{
throw new Exception (e.getMessage());
}
return bStatus;
}
public String[] getMappings() {
return new String[] { "Product.hbm.xml" };
}
public static Test suite() throws Exception {
return new TestSuite(InstalledProductTest.class);
}
private Session getSession()throws Exception
{
if(s==null)
s=openSession();
return s;
}
private String rand(String s)
{
sBuf.setLength(0);
sBuf.append(s).append(random());
return sBuf.toString();
}
private int random()
{
return randomNumber.nextInt(RAND_MAX_LIMIT);
}
private int random(int limit)
{
return randomNumber.nextInt(limit);
}
private void commit()throws Exception
{
commit(0);
}
private void commit(int index)throws Exception
{
if(index==0 || index%COMMIT_COUNT==0) conn.commit();
}
private InputStream getResourceStream(String fileName)
{
ClassLoader loader = this.getClass().getClassLoader();
InputStream is = loader.getResourceAsStream(fileName);
return is;
}
public void testMain() throws Exception
{
doTestInit();
doTestMainJDBC();
doTestMainHibernate();
doTestFinal();
}
private void doTestMainHibernate() throws Exception
{
doTestHibernateInsertIntoInstalledProduct();
doTestHibernateRepeatSelectMultipleCondition1();
doTestHibernateRepeatSelectMultipleCondition2();
doTestHibernateRepeatSelectOneInstalledProduct();
}
private void doTestMainJDBC() throws Exception
{
doTestJDBCInsertIntoInstalledProduct();
doTestJDBCRepeatSelectMultipleCondition();
doTestJDBCRepeatSelectOneInstalledProduct();
}
private void doTestInit() throws Exception
{
openSession();
System.out.println("Obtained Session");
testProps = new Properties();
try{
testProps.load(getResourceStream("InstalledProduct.properties"));
COMMIT_COUNT = Integer.parseInt(testProps.getProperty("commitcount"));
batchSize = Integer.parseInt(testProps.getProperty("batchsize"));
distinctUcs = Integer.parseInt(testProps.getProperty("distinctucs"));
productPerUcs = Integer.parseInt(testProps.getProperty("productperucs"));
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
pstmt_getId = conn.prepareStatement("select @@identity");
}
private void doTestHibernateInsertIntoInstalledProduct() throws Exception
{
// Delete the existing records
cleanUp("Hibernate");
long ucs_find=0; // Time taken to find an UCS
long ucs_add=0; // Time taken to add an UCS
long product_find_present=0; // Time taken to determine that a given product exists
long product_find_absent=0; // Time taken to determine that a specified product is absent
long product_add=0; // Time taken to add a product
long directory_find_present=0;
long directory_find_absent=0;
long directory_add=0;
long ip_add=0;
long start = 0;
long end=0;
int ucs_find_count=0;
int ucs_add_count=0;
int dir_find_present_count=0;
int dir_find_absent_count=0;
int dir_add_count=0;
int product_find_present_count=0;
int product_find_absent_count=0;
int product_add_count=0;
int ip_add_count=0;
/*
Query q_product=s.createSQLQuery("select {p}.id$ as {p.id} from t$Product {p} where {p}.vendor=? and {p}.name=? and {p}.version=?","p",Product.class).setFirstResult(0).setMaxResults(1).setCacheable(true).setCacheRegion("frontpages");
Query q_ucs=s.createSQLQuery("select {u}.id$ as {u.id}, {u}.name as {u.name} from t$UnitaryComputerSystem {u} where {u}.name=? ","u",UnitaryComputerSystem.class).setFirstResult(0).setMaxResults(1).setCacheable(true).setCacheRegion("frontpages");
Query q_directory=s.createSQLQuery("select {d}.id as {d.id}, {d}.path as {d.path} from t$Directory {d} where {d}.path=? ","d",Directory.class).setFirstResult(0).setMaxResults(1).setCacheable(true).setCacheRegion("frontpages");
*/
Query q_product=s.createQuery("from Product as p where p.vendor=? and p.name=? and p.version=?");
Query q_ucs=s.createQuery("from UnitaryComputerSystem as u where u.name=?");
Query q_directory=s.createQuery("from Directory as d where d.path=?");
//InstalledProduct NullInstalledProduct = new InstalledProduct();
BufferedReader brProduct=new BufferedReader(new FileReader("product.txt"));
BufferedReader brDirectory=new BufferedReader(new FileReader("directory.txt"));
BufferedReader brUcs=new BufferedReader(new FileReader("ucs.txt"));
UnitaryComputerSystem ComputerSystem=null;
//Directory DirectoryId =null;
//Product product=null;
Vector vEvictVector=new Vector();
System.out.println(" STARTING ON HIBERNATE INSERT");
System.out.println("distinctUcs: "
+ distinctUcs);
for(int i=0;i<distinctUcs;i++)
{
String ucsname=brUcs.readLine();
try
{
q_ucs.setString(0,ucsname);
start=System.currentTimeMillis();
ComputerSystem=(UnitaryComputerSystem)q_ucs.uniqueResult();
if(ComputerSystem==null) throw nrfe;
ucs_find+=System.currentTimeMillis()-start;
ucs_find_count++;
}
catch(Exception aie)
{
if((aie instanceof IndexOutOfBoundsException)|| (aie instanceof TransientObjectException) ||(aie instanceof NoRecordFoundException))
{
ucs_find+=System.currentTimeMillis()-start;
ComputerSystem=new UnitaryComputerSystem(ucsname);
start=System.currentTimeMillis();
s.save(ComputerSystem);
ucs_add+=System.currentTimeMillis()-start;
ucs_add_count++;
java.util.Date d=new java.util.Date(System.currentTimeMillis());
System.out.println("HIBERNATE:"+d.toString()+" UCS:"+ucsname);
}
else throw aie;
}
vEvictVector.add(ComputerSystem);
for(int j=0;j<productPerUcs;j++)
{
StringTokenizer productLineTokenizer=new StringTokenizer(brProduct.readLine(),",");
String Vendor= productLineTokenizer.nextToken();
String Name= productLineTokenizer.nextToken();
String Version= productLineTokenizer.nextToken();
String Category= productLineTokenizer.nextToken();
String HelpLink= productLineTokenizer.nextToken();
Product product=null;
//product
try
{
q_product.setString(0,Vendor);
q_product.setString(1,Name);
q_product.setString(2,Version);
start=System.currentTimeMillis();
product=(Product)q_product.uniqueResult();
if(product==null) throw nrfe;
product_find_present+=System.currentTimeMillis()-start;
product_find_present_count++;
}
catch(Exception aie2)
{
if((aie2 instanceof IndexOutOfBoundsException)|| (aie2 instanceof TransientObjectException) || (aie2 instanceof NoRecordFoundException))
{
product_find_absent+=System.currentTimeMillis()-start;
product_find_absent_count++;
product = new Product(Vendor,Name,Version,Category,HelpLink);
start=System.currentTimeMillis();
s.save(product);
product_add+=System.currentTimeMillis()-start;
product_add_count++;
}
else throw aie2;
}
vEvictVector.add(product);
//Directory
Directory DirectoryId = null;
String dir=brDirectory.readLine();
try
{
q_directory.setString(0, dir);
start=System.currentTimeMillis();
DirectoryId=(Directory)q_directory.uniqueResult();
if(DirectoryId==null) throw nrfe;
directory_find_present+=System.currentTimeMillis()-start;
dir_find_present_count++;
}
catch(Exception ae3)
{
if((ae3 instanceof IndexOutOfBoundsException)|| (ae3 instanceof TransientObjectException)||(ae3 instanceof NoRecordFoundException))
{
directory_find_absent+=System.currentTimeMillis()-start;
dir_find_absent_count++;
DirectoryId=new Directory(dir);
start=System.currentTimeMillis();
s.save(DirectoryId);
directory_add+=System.currentTimeMillis()-start;
dir_add_count++;
}
else throw ae3;
}
vEvictVector.add(DirectoryId);
InstalledProduct ip = new InstalledProduct(
rand("ProductIdentifier"),
rand("InternalVersion"),
new Integer(j/10),//Language
rand("UnInstallString"),
rand("InstallationSource"),
rand("FriendlyName"),
rand("SupportPack"),
rand("ProductEdition"),
new Long(random()),//LastExecutionTime;
new Integer(random()),//FrequencyOfUsage;
rand("Description"),
new Integer(random()),//"SoftwareType"),
rand("PackageGUID"),
rand("AdrMsiKey"),
new Integer(random()),//InformationSrc_Bitmap;
ComputerSystem,
product,
DirectoryId,
null//NullInstalledProduct
);
start=System.currentTimeMillis();
s.save(ip);
ip_add+=System.currentTimeMillis()-start;
ip_add_count++;
//s.evict(ip);
//s.evict(DirectoryId);
//s.evict(product);
vEvictVector.add(ip);
}
commit();
for (int k=0;k<vEvictVector.size();k++)
{
Object o = vEvictVector.remove(k);
s.evict(o);
o = null;
}
}
System.out.println("HIBERNATE Ucs Find:"+ucs_find+" Findcount:"+ucs_find_count+" ucs Add:"+ucs_add+" Ucs Add Count:"+ucs_add_count);
System.out.println("HIBERNATE Product FindPresent:"+product_find_present+" Presentcount:"+product_find_present_count+" FindAbsent:"+product_find_absent+" AbsentCount:"+product_find_absent_count+" Add:"+product_add+" Add Count:"+product_add_count);
System.out.println("HIBERNATE Dir FindPresent:"+directory_find_present+" Presentcount:"+dir_find_present_count+" FindAbsent:"+directory_find_absent+" AbsentCount:"+dir_find_absent_count+" Add:"+directory_add+" Add Count:"+dir_add_count);
System.out.println("HIBERNATE IP Add:"+ip_add+" IP Add Count:"+ip_add_count);
recordTime("Hibernate", "InsertUCS_Check", ucs_find_count , ucs_find);
recordTime("Hibernate", "InsertProduct_Check", product_find_present_count+product_find_absent_count , product_find_present+product_find_absent);
recordTime("Hibernate", "InsertDirectory_Check", dir_find_present_count+dir_find_absent_count, directory_find_present+directory_find_absent);
recordTime("Hibernate", "InsertUCS", ucs_add_count , ucs_add);
recordTime("Hibernate", "InsertProduct", product_add_count , product_add);
recordTime("Hibernate", "InsertDirectory", dir_add_count , directory_add);
recordTime("Hibernate", "InsertInstalledProduct", ip_add_count , ip_add);
brUcs.close();
brDirectory.close();
brProduct.close();
}
private void doTestHibernateSelectMultipleCondition1(Query q) throws Exception
{
//String [] aliases = {"dir", "prod", "ucs", "ip"};
//Class [ ] classes = {Directory.class, Product.class, UnitaryComputerSystem.class, InstalledProduct.class};
long startTime = System.currentTimeMillis();
Iterator result = q.iterate();
long endTime = System.currentTimeMillis();
int numRecords = 0;
if (result != null)
{
while (result.hasNext())
{
result.next();
numRecords++;
}
}
recordTime("Hibernate", "SelectMultipleCondition-Iterator", numRecords, endTime -startTime);
}
private void doTestHibernateRepeatSelectMultipleCondition1() throws Exception
{
Query q = s.createQuery("from InstalledProduct as ip, Directory as dir, Product as prod, UnitaryComputerSystem as ucs where ip.Product.id=prod.id and ip.DirectoryId.id=dir.id and ip.ComputerSystem.id=ucs.id and ip.Language=7");
for(int i = 0; i < 3; i++)
doTestHibernateSelectMultipleCondition1(q);
}
private void doTestHibernateSelectMultipleCondition2(Query q) throws Exception
{
//String [] aliases = {"dir", "prod", "ucs", "ip"};
//Class [ ] classes = {Directory.class, Product.class, UnitaryComputerSystem.class, InstalledProduct.class};
long startTime = System.currentTimeMillis();
List result = q.list();
long endTime = System.currentTimeMillis();
int numRecords = 0;
if (result != null)
numRecords = result.size();
recordTime("Hibernate", "SelectMultipleCondition-List", numRecords, endTime -startTime);
}
private void doTestHibernateRepeatSelectMultipleCondition2() throws Exception
{
Query q = s.createQuery("from InstalledProduct as ip, Directory as dir, Product as prod, UnitaryComputerSystem as ucs where ip.Product.id=prod.id and ip.DirectoryId.id=dir.id and ip.ComputerSystem.id=ucs.id and ip.Language=7");
for(int i = 0; i < 3; i++)
doTestHibernateSelectMultipleCondition2(q);
}
private boolean doTestHibernateSelectOneInstalledProduct(Query q_ip) throws Exception
{
boolean recFound = false;
long startTime = System.currentTimeMillis();
Iterator result = q_ip.iterate();
long endTime = System.currentTimeMillis();
int numRecords = 0;
if (result != null && result.hasNext())
{
recFound = true;
while(result.hasNext())
{
result.next();
numRecords++;
}
}
recordTime("Hibernate", "SelectOneInstalledProduct", numRecords, endTime -startTime);
return recFound;
}
private void doTestHibernateRepeatSelectOneInstalledProduct() throws Exception
{
Query q_ip = s.createQuery("from InstalledProduct as ip, Product as p where ip.Product = p.id and p.vendor=? and p.name=? and p.version=?");
q_ip.setString(0, "DUMMY_VENDOR4517");
q_ip.setString(1, "DUMMY_NAME6072");
q_ip.setString(2, "DUMMY_VERSION4380");
assertTrue(doTestHibernateSelectOneInstalledProduct(q_ip));
q_ip.setString(0, "DUMMY_VENDOR1537");
q_ip.setString(1, "DUMMY_NAME2333");
q_ip.setString(2, "DUMMY_VERSION9071");
assertFalse(doTestHibernateSelectOneInstalledProduct(q_ip));
q_ip.setString(0, "DUMMY_VENDOR3041");
q_ip.setString(1, "DUMMY_NAME5175");
q_ip.setString(2, "DUMMY_VERSION4688");
assertTrue(doTestHibernateSelectOneInstalledProduct(q_ip));
}
class NoRecordFoundException extends Exception
{
public NoRecordFoundException (String msg)
{
super(msg);
}
}
NoRecordFoundException nrfe=new NoRecordFoundException("No record found");
private long getId(PreparedStatement psmnt) throws SQLException, NoRecordFoundException
{
long id=-1;
ResultSet rset=psmnt.executeQuery();
if(rset.next())
id=rset.getLong(1);
rset.close();
if(id==-1)
throw nrfe;
return id;
}
private long getGeneratedId() throws SQLException, NoRecordFoundException
{
ResultSet rset=pstmt_getId.executeQuery();
long id = -1;
if(rset.next())
id = rset.getLong(1);
rset.close();
return id;
}
private long getGeneratedId(PreparedStatement psmnt) throws SQLException, NoRecordFoundException
{
long id=-1;
ResultSet rset=psmnt.getGeneratedKeys();
if(rset.next())
id=rset.getLong(1);
rset.close();
if(id==-1)
throw nrfe;
return id;
}
private void doTestJDBCInsertIntoInstalledProduct() throws Exception
{
cleanUp("JDBC");
PreparedStatement q_product=conn.prepareStatement("select id$ from t$Product where vendor=? and name=? and version=?");
PreparedStatement a_product=conn.prepareStatement("INSERT INTO t$Product(vendor,name,version,category,helplink) values(?,?,?,?,?)");//, Statement.RETURN_GENERATED_KEYS);
PreparedStatement q_ucs=conn.prepareStatement("select id$ from t$UnitaryComputerSystem where name=?");
PreparedStatement a_ucs=conn.prepareStatement("insert into t$UnitaryComputerSystem(name) values(?)");//, Statement.RETURN_GENERATED_KEYS);
PreparedStatement q_directory=conn.prepareStatement("select id from t$Directory where path=?");
PreparedStatement a_directory=conn.prepareStatement("insert into t$Directory(path) values (?)");//, Statement.RETURN_GENERATED_KEYS);
PreparedStatement a_ip=new BatchPreparedStatement(conn.prepareStatement("insert into t$InstalledProduct(computersystem,product,directoryid,productidentifier,internalversion,language,uninstallstring,installationsource,friendlyname,supportpack,productedition,lastexecutiontime,frequencyofusage,description,softwaretype,packageguid,adrmsikey,informationsrc_bitmap,fk_pinstanceid) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) "));
BufferedReader brProduct = new BufferedReader(new FileReader("product.txt"));
BufferedReader brDirectory= new BufferedReader(new FileReader("directory.txt"));
BufferedReader brUcs = new BufferedReader(new FileReader("ucs.txt"));
long ucs_find=0;
long ucs_add=0;
long product_find_present=0;
long product_find_absent=0;
long product_add=0;
long directory_find_present=0;
long directory_find_absent=0;
long directory_add=0;
long ip_add=0;
long start=0;
long end=0;
int ucs_find_count=0;
int ucs_add_count=0;
int dir_find_present_count=0;
int dir_find_absent_count=0;
int dir_add_count=0;
int product_find_present_count=0;
int product_find_absent_count=0;
int product_add_count=0;
int ip_add_count=0;
long productid=-1;
long directoryid=-1;
long ucsid=-1;
System.out.println(" STARTING ON JDBC INSERT");
for(int i=0;i<distinctUcs;i++)
{
String ucsname=brUcs.readLine();
try
{
q_ucs.setString(1,ucsname);
start=System.currentTimeMillis();
ucsid=getId(q_ucs);
ucs_find+=System.currentTimeMillis()-start;
ucs_find_count++;
}
catch(Exception aie)
{
if((aie instanceof NoRecordFoundException))
{
ucs_find+=System.currentTimeMillis()-start;
a_ucs.setString(1,ucsname);
start=System.currentTimeMillis();
a_ucs.executeUpdate();
ucsid=getGeneratedId();
ucs_add+=System.currentTimeMillis()-start;
ucs_add_count++;
java.util.Date d=new java.util.Date(System.currentTimeMillis());
System.out.println("JDBC "+d.toString()+" Added ucs "+ucsname);
}
else throw aie;
}
for(int j=0;j<productPerUcs;j++)
{
StringTokenizer productLineTokenizer=new StringTokenizer(brProduct.readLine(),",");
String Vendor= productLineTokenizer.nextToken();
String Name= productLineTokenizer.nextToken();
String Version= productLineTokenizer.nextToken();
String Category= productLineTokenizer.nextToken();
String HelpLink= productLineTokenizer.nextToken();
//product
try
{
q_product.setString(1,Vendor);
q_product.setString(2,Name);
q_product.setString(3,Version);
start=System.currentTimeMillis();
productid=getId(q_product);
product_find_present+=System.currentTimeMillis()-start;
product_find_present_count++;
}
catch(Exception aie2)
{
if(aie2 instanceof NoRecordFoundException)
{
product_find_absent+=System.currentTimeMillis()-start;
product_find_absent_count++;
a_product.setString(1,Vendor);
a_product.setString(2,Name);
a_product.setString(3,Version);
a_product.setString(4,Category);
a_product.setString(5,HelpLink);
start=System.currentTimeMillis();
a_product.executeUpdate();
productid=getGeneratedId();
product_add+=System.currentTimeMillis()-start;
product_add_count++;
}
else throw aie2;
}
//Directory
String dir=brDirectory.readLine();
try
{
//DirectoryId=(Directory)s.find("from org.hibernate.test.Directory as directory where directory.path=?",dir,Hibernate.STRING).get(0);
q_directory.setString(1, dir);
start=System.currentTimeMillis();
directoryid=getId(q_directory);
directory_find_present+=System.currentTimeMillis()-start;
dir_find_present_count++;
}
catch(Exception ae3)
{
if(ae3 instanceof NoRecordFoundException)
{
directory_find_absent+=System.currentTimeMillis()-start;
dir_find_absent_count++;
a_directory.setString(1,dir);
start=System.currentTimeMillis();
a_directory.executeUpdate();
directoryid=getGeneratedId();
directory_add+=System.currentTimeMillis()-start;
dir_add_count++;
}
else throw ae3;
}
a_ip.setLong(1,ucsid);
a_ip.setLong(2,productid);
a_ip.setLong(3,directoryid);
a_ip.setString(4,rand("ProductIdentifier"));
a_ip.setString(5,rand("InternalVersion"));
a_ip.setInt(6, j/10);//Language
a_ip.setString(7,rand("UnInstallString"));
a_ip.setString(8,rand("InstallationSource"));
a_ip.setString(9,rand("FriendlyName"));
a_ip.setString(10,rand("SupportPack"));
a_ip.setString(11,rand("ProductEdition"));
a_ip.setLong(12,new Long(random()).longValue());//LastExecutionTime;
a_ip.setInt(13,new Integer(random()).intValue());//FrequencyOfUsage;
a_ip.setString(14,rand("Description"));
a_ip.setInt(15,new Integer(random()).intValue());//"SoftwareType"),
a_ip.setString(16,rand("PackageGUID"));
a_ip.setString(17,rand("AdrMsiKey"));
a_ip.setInt(18,new Integer(random()).intValue());//InformationSrc_Bitmap;
a_ip.setNull(19,java.sql.Types.INTEGER);
start=System.currentTimeMillis();
a_ip.addBatch();
//if(j%batchSize == 0) batchUpdate(a_ip);
ip_add+=System.currentTimeMillis()-start;
ip_add_count++;
}
a_ip.executeBatch();
conn.commit();
}
System.out.println("JDBC Ucs Find:"+ucs_find+" Ucs Findcount:"+ucs_find_count+" ucs Add:"+ucs_add+" Ucs Add Count:"+ucs_add_count);
System.out.println("JDBC Product FindPresent:"+product_find_present+" Presentcount:"+product_find_present_count+" FindAbsent:"+product_find_absent+" AbsentCount:"+product_find_absent_count+" Add:"+product_add+" Add Count:"+product_add_count);
System.out.println("JDBC Dir FindPresent:"+directory_find_present+" Presentcount:"+dir_find_present_count+" FindAbsent:"+directory_find_absent+" AbsentCount:"+dir_find_absent_count+" Add:"+directory_add+" Add Count:"+dir_add_count);
System.out.println("JDBC IP Add:"+ip_add+" IP Add Count:"+ip_add_count);
recordTime("JDBC", "InsertUCS_Check", ucs_find_count , ucs_find);
recordTime("JDBC", "InsertProduct_Check", product_find_present_count+product_find_absent_count , product_find_present+product_find_absent);
recordTime("JDBC", "InsertDirectory_Check", dir_find_present_count+dir_find_absent_count, directory_find_present+directory_find_absent);
recordTime("JDBC", "InsertUCS", ucs_add_count , ucs_add);
recordTime("JDBC", "InsertProduct", product_add_count , product_add);
recordTime("JDBC", "InsertDirectory", dir_add_count , directory_add);
recordTime("JDBC", "InsertInstalledProduct", ip_add_count , ip_add);
brUcs.close();
brDirectory.close();
brProduct.close();
}
private void doTestJDBCSelectMultipleCondition(PreparedStatement q_ip) throws Exception
{
long startTime = System.currentTimeMillis();
ResultSet result = q_ip.executeQuery();
long endTime = System.currentTimeMillis();
int numRecords = 0;
while(result.next())
numRecords++;
result.close();
recordTime("JDBC", "SelectMultipleCondition", numRecords, endTime -startTime);
}
private void doTestJDBCRepeatSelectMultipleCondition() throws Exception
{
// PreparedStatement q_ip = conn.prepareStatement("select ip.InternalVersion ip_int_ver, dir.path dir_path, prod.name product_name, ucs.name ucs_name from t$InstalledProduct as ip, t$Directory as dir, t$Product as prod, t$UnitaryComputerSystem as ucs where ip.Product = prod.id$ and ip.directoryid = dir.id and ip.ComputerSystem=ucs.id$ and ip.Language>9600");
PreparedStatement q_ip = conn.prepareStatement("select * from t$InstalledProduct as ip, t$Directory as dir, t$Product as prod, t$UnitaryComputerSystem as ucs where ip.Product = prod.id$ and ip.directoryid = dir.id and ip.ComputerSystem=ucs.id$ and ip.Language=7");
for(int i = 0; i < 3; i++)
doTestJDBCSelectMultipleCondition(q_ip);
q_ip.close();
}
private boolean doTestJDBCSelectOneInstalledProduct(PreparedStatement pstmt) throws Exception
{
boolean recFound = false;
int numRecords = 0;
long startTime = 0;
long endTime = 0;
try
{
startTime = System.currentTimeMillis();
ResultSet result = pstmt.executeQuery();
endTime = System.currentTimeMillis();
while(result.next())
{
numRecords++;
recFound = true;
}
result.close();
}
catch (SQLException e)
{
throw new Exception(e);
}
recordTime("JDBC", "SelectOneInstalledProduct", numRecords, endTime-startTime);
return recFound;
}
private void doTestJDBCRepeatSelectOneInstalledProduct() throws Exception
{
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM t$InstalledProduct, t$Product WHERE t$InstalledProduct.Product = t$Product.id$ and t$Product.vendor=? and t$Product.name=? and t$Product.version=?");
pstmt.setString(1, "DUMMY_VENDOR4517");
pstmt.setString(2, "DUMMY_NAME6072");
pstmt.setString(3, "DUMMY_VERSION4380");
assertTrue(doTestJDBCSelectOneInstalledProduct(pstmt));
pstmt.setString(1, "DUMMY_VENDOR1537");
pstmt.setString(2, "DUMMY_NAME2333");
pstmt.setString(3, "DUMMY_VERSION9071");
assertFalse(doTestJDBCSelectOneInstalledProduct(pstmt));
pstmt.setString(1, "DUMMY_VENDOR3041");
pstmt.setString(2, "DUMMY_NAME5175");
pstmt.setString(3, "DUMMY_VERSION4688");
assertTrue(doTestJDBCSelectOneInstalledProduct(pstmt));
pstmt.close();
}
private void doTestFinal() throws Exception
{
pstmt_getId.close();
closeSession();
System.out.println("Closed Session");
}
public void recordTime(String approach, String action, int numRecords, long duration) throws Exception
{
try
{
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO t$Time (Approach, Action, ResultSize, Duration) VALUES (?, ?, ?, ?)");
pstmt.setString(1, approach);
pstmt.setString(2, action);
pstmt.setInt(3, numRecords);
pstmt.setLong(4, duration);
pstmt.executeUpdate();
pstmt.close();
conn.commit();
}
catch(SQLException e)
{
throw new Exception(e);
}
}
}
Full stack trace of any exception that occurs:
Name and version of the database you are using:
Sybase adaptive server anywhere 8
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt: