Navigation trail:Torque


Q: What is Village?

A: See the Village homepage for that infomation.


Q: Torque uses Village 2.0. I can't find any reference to that version on the Village page.

A: Village 2.0 has not been released. You can get the source by getting the latest CVS version. Instructions for doing this are on the Village homepage. When you build the project, you will have a village-2.0-dev jar file.


Q: After installing the torque plugin, xdoc generation doesn't work

A: This is a known problem. To workaround this problem, remove the maven-torque-plugin-3.1-dev.jar and the maven-torque-plugin-3.1-dev directory from you ${maven.home}/plugins directory.

  • This workaround is not very useful now that turbine-2 HEAD requires the torque plugin for maven - "maven site" now fails with or without the plugin installed. – ScottEade 2003-01-28
  • The change that introdiced the problem has been backed out. – ScottEade 2003-02-05

Q: How can I configure Torque to use a JNDI connection

A: Read the Torque Howto for indepth information. However, here is the short howto!

  • Torque.properties:
 torque.database.default.adapter=mssql 
 torque.dsfactory.default.factory=org.apache.torque.dsfactory.JndiDataSourceFactory 
 torque.dsfactory.default.jndi.path=java:comp/env/jdbc/fortius 
  • web.xml (In the right order according to the servlet spec):
 <resource-ref> 
  <description> 
    Resource reference to a factory for java.sql.Connection 
    instances that may be used for talking to a particular 
    database that is configured in the server.xml file. 
  </description> 
  <res-ref-name> 
    jdbc/fortius 
  </res-ref-name> 
  <res-type> 
    org.apache.torque.pool.TorqueClassicDataSource 
  </res-type> 
  <res-auth> 
    Container 
  </res-auth> 
 </resource-ref> 
  • server.xml (inside your Context):
 <Resource name="jdbc/fortius" scope="Shareable" type="javax.sql.DataSource"/> 
 <ResourceParams name="jdbc/fortius"> 
  <parameter> 
    <name>validationQuery>/name> 
    <value>SELECT 1>/value> 
  </parameter> 
  <parameter> 
    <name>maxWait>/name> 
    <value>5000>/value> 
  </parameter> 
  <parameter> 
    <name>maxActive>/name> 
    <value>4>/value> 
  </parameter> 
  <parameter> 
    <name>password>/name> 
    <value>mypassword;/value> 
  </parameter> 
  <parameter> 
    <name>url>/name> 
    <value>jdbc:microsoft:sqlserver://cuzco:1433;DatabaseName=fortius;SelectMethod=cursor>/value> 
  </parameter> 
  <parameter> 
    <name>driverClassName>/name> 
    <value>com.microsoft.jdbc.sqlserver.SQLServerDriver>/value> 
  </parameter> 
  <parameter> 
    <name>maxIdle>/name> 
    <value>2>/value> 
  </parameter> 
  <parameter> 
    <name>username>/name> 
    <value>SilverUserLogin>/value> 
  </parameter> 
 </ResourceParams> 
  • Libraries. Make sure you put your Jar's that are need for the driver where they can be reached. For Tomcat, put them in /common/lib, otherwise Tomcat can not create the connection.
  • Debugging

If all else fails, first try and verify that you are able to directly create the datasource. I wrote a little action called DB.java that I could call from torque with a single action:

    public void doPerform(RunData data, Context context) 
        throws Exception { 
        try { 

            Log.debug("DB.doPerform called"); 
            String foo = ''''''; 
            javax.naming.Context initCtx = new InitialContext(); 
            javax.naming.Context envCtx = (javax.naming.Context) initCtx.lookup("java:comp/env"); 
            DataSource ds = 
                    (DataSource) envCtx.lookup("jdbc/fortius"); 
            if (ds != null) { 
                Connection conn = ds.getConnection(); 
                if (conn != null) { 
                    System.out.println("Got Connection " + conn.toString()); 
                    Statement stmt = conn.createStatement(); 
                    ResultSet rst = 
                            stmt.executeQuery( 
                            "select * from kinase"); 
                    if (rst.next()) { 
                        foo = rst.getString(2); 
                        System.out.println("foo:" + foo); 
                    } 

                    conn.close(); 
                } 
            } 

        } 
        catch (Exception e) { 
            Log.error(e); 
            throw e; 
        } 
    } 

EricPugh


Q: How can I execute a stored procedure

A: You can use some of the functionality exposed by the Village library to execute and deal with the result set of your stored procedure:

  import com.workingdogs.village.Record; 

  String SQL = "exec myStroedProc 4, 'hello'"; 
  List records = WorkorderPeer.executeQuery(SQL); 

  Vector kinaseATPConcs = new Vector(); 
  for (Iterator i = records.iterator(); i.hasNext();) { 
    Record record = (Record) i.next(); 
    ["KinaseATPConc"] kinaseATPConc = new ["KinaseATPConc"](); 
    kinaseATPConc.setKinaseId(record.getValue("kinase_id").asString()); 
    kinaseATPConc.setAtpConc(record.getValue("atp_conc").asDouble()); 

    kinaseATPConcs.add(kinaseATPConc); 
  } 

What else you may notice is that if you do something similar with a complex sql query:

  String SQL = "select top 50 distinct kinase_id,atp_conc from reaction where workorder_id = " + workorder.getWorkorderId() + "  and reaction.result is null and reaction.daughterboard_id is null"); 

EricPugh


Q: How to add P6Spy for printing SQL

A: 1) add the p6spy.jar to your application classpath

   2) copy the "spy.properties" into your classpath (e.g. same location as log4j.properties) 
   3) set the "realdriver" in spy.properties, e.g. realdriver=oracle.jdbc.driver.OracleDriver for Oracle 
   4) change the Torque.properties, e.g. "torque.dsfactory.XXX.connection.driver = com.p6spy.engine.spy.P6SpyDriver" to use the proxy JDBC drivers 
   5) run your application 
   6) P6Spy creates a spy.log in your current directory 

– Siegfried Goeschl


Q: doDelete() is throwing a TorqueException. What's up?

A: doDelete() throws a TorqueException with message "You must specify KeyDef attributes for this TableData'*Set in order to delete a Record" when the schema for the table does not contain a primary key. If the table has no primary key, doDelete() may not be used to delete from it. *What should be used in that case'?

– Gary Shea

  • No labels