Saturday, February 5, 2011

JavaScript-friendly alternative to the f(x) = y JScript idiom that's used when setting CDO.Message options

I have an ASP page written in JScript that sends e-mails using CDO.Message. For specifying an SMTP server (and other options) I'm doing something like this:

mail.Configuration.Fields.Item(
    "http://schemas.microsoft.com/cdo/configuration/smtpserver") =
    "smtp.example.com";

Now, here comes the catch. I have this code in a stand-alone include file that I include in an HTML page as JavaScript so that I can run unit tests against it in a browser (using JsUnit etc.). I have JavaScript mock objects (Server, Request, etc.) that create a mock ASP environment for the included JScript code. The only problem I have left is with the CDO.Message option setting. Since the f(x) = y syntax that's used in the above code excerpt is not valid JavaScript (invalid left-hand operand), I can't run this piece of code (as it is) within a browser. I'm currently simply bypassing it in my unit test with a conditional that detects whether the environment is truly ASP.

I don't think that there's a JavaScript workaround to this. I'm looking for an alternative syntax (that may use the ActiveX interfaces differently) to setting CDO.Message options that would also be syntactically valid JavaScript.

  • I figured out the answer when looking at the C++ code example at http://msdn.microsoft.com/en-us/library/ms526318(EXCHG.10).aspx.

    The solution is to make the assignment explicitly to the Value property:

    mail.Configuration.Fields.Item(
        "http://schemas.microsoft.com/cdo/configuration/smtpserver").Value =
        "smtp.example.com";
    

    This way, the code above is valid JavaScript than can be tested with a mock Configuration object.

    From Ates Goral

How do I include filtered rowcounts from two other tables in a query of a third?

I have a MySql database with three tables I need to combine in a query: schedule, enrolled and waitlist. They are implementing a basic class enrollment system.

Schedule contains the scheduled classes. When a user enrolls in a class, their user account id and the id of the scheduled class are stored in enrolled. If a class is at capacity, they are stored in waitlist instead. All three tables share a scheduleId column which identifies each class.

When I query the schedule table, I need to also return enrolled and waitlist columns that represent the number of users enrolled and waiting for that particular scheduleId.

A preliminary query I came up with to accomplish this was:

select s.id, s.classDate, s.instructor, COUNT(e.id) as enrolled
from schedule as s
left outer join enrolled as e
on s.id = e.scheduleId
group by s.id

which works ok for one or the other, but obviously I can't get the values for both the enrolled and waitlist tables this way. Can anybody suggest a good way of doing this?

  • Two quick ways:

    1- Use COUNT(DISTINCT e.id), COUNT(DISTINCT w.id) to get the number of unique instances in each table, then join on both. This is possibly hideously inefficient.

    2- Use subqueries in the FROM clause (only works in MySQL 5.0 and later):

    SELECT s.id, s.classDate, s.instructor, tmpE.c AS enrolled, tmpW.c AS waiting
    FROM
      schedule AS s,
      ( SELECT scheduleID, COUNT(*) AS c FROM enrolled GROUP BY scheduleID ) AS tmpE,
      ( SELECT scheduleID, COUNT(*) AS c FROM waiting GROUP BY scheduleID ) AS tmpW
    WHERE
        s.id = e.scheduleID
        AND s.id = w.scheduleID
    GROUP BY s.id
    

    I may have missed a left join in there, though.

    From kyle
  • Use nested SELECT queries. Assuming a bit about your schema, how about something like this (might not work on some flavors of SQL):

    select s.id, s.classDate, s.instructor, 
           (select COUNT(e.id) from enrolled e where e.scheduleId = s.id) as enrolled,
           (select COUNT(w.id) from waitlist w where w.scheduleId = s.id) as waiting
    from schedule as s
    group by s.id
    
    From lc
  • I would do it with another left join and two inline count(distincts)

    select s.id, s.classDate, s.instructor ,
    count(distinct e.id) as enrolled,
    count(distinct w.id) as waiting
    from schedule as s
    left outer join enrolled as e
    on s.id = e.scheduleID
    left outer join waitlist as w
    on s.id = w.scheduleID
    group by s.id
    

    When I ran this approach versus the subqueries it executed about twice as fast, but I am looking at a pretty small result set.

    From cmsjr

Discover Microsoft SQL Servers on an Internal Network

What is the proper way using .NET to discover the Microsoft SQL Servers on your internal network? I think the SQL Management interface back in the SQL Server 7 days gave you a list of servers in a dropdown. How could I do the same thing with .NET 3.5.

Testrunner/framework for both .Net and Javascript?

My team develops software using multiple languages. We have a server app in .Net and much of our client code is written in JavaScript. (The client code is actually for xulrunner applications, which don't have good testing tools right now, but I hope that is overcome someday.)

I like the idea, though, of having one testing tool that can run tests for all of our code. Throwing out the xulrunner problem right now, and just considering .net and Javascript - are there any options for testing and automation to cover both of these areas? Most of the popular testing tools I have seen seem to be .Net-focused.

Or should I drop my desire to have one testing tool to rule them all, and simply choose the best of breed for each?

  • I'm using a single testing framework in a somewhat similar scenario - using NUnit to test both .Net and unmanaged C++ code. I just write the tests for the later in managed C++. This was nicer than using CppUnit IMO.

    But in your case, there isn't a similar bridge between the .Net and JavaScript worlds that you could use. So you are likely better off using dedicated test frameworks for each platform.

    From oefe

Hibernate session handling in spring web services

I am using spring-ws with Jaxb2Marshaller, PayloadRootAnnotationMethodEndpointMapping and GenericMarshallingMethodEndpointAdapter to configure my web services via the @Endpoint and @PayloadRoot annotations.

When I try to use the DAO's of my project I am able to load objects from the database but as soon as I try to access properties inside my service that should be lazily loaded I get a org.hibernate.LazyInitializationException - could not initialize proxy - no Session.

In my spring-mvc web application the OpenSessionInViewInterceptor handles the sessions. How do I configure my web service project to automatically create a Hibernate session for every web service call?

  • Wrap a org.springframework.aop.framework.ProxyFactoryBean around the object in the spring context that needs the hibernate session to be present.

    This article http://springtips.blogspot.com/2007/06/spring-and-hibernate.html shows how to do it.

    If you experience problems because of lazy-loaded collections when using sessions this way there are at least 2 possible fixes:

    • Add a Hibernate.initialize() call to the collection in code that is executed with the Hibernate session available - http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Hibernate.html#initialize(java.lang.Object)
    • Use a non-lazy collection by adding lazy="false" to the mapping - watch out when using this option, you can easily force hibernate to load your whole database with a couple of badly placed lazy="false" options.
    Thomas Einwaller : I found out that the DAO works but lazy loading fails ... the tip in the link does not help me
    Simon Groenewolt : Added 2 possible fixes for this to my answer.
  • In the meanwhile I found a solution. This forum entry gave me the hint:

    http://forum.springframework.org/showthread.php?t=50284

    Basically I added the @Transactional annotations to my web service implementation class. The tricky part was to tell spring to use the original class (not the proxy created by tx:annotation-driven) which I achieved by using the following configuration:

    <bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping" >
        <property name="order" value="1" />
    </bean>
    
    <tx:annotation-driven mode="proxy" order="200" proxy-target-class="true" />
    

    The order of the configuration statements seems important too.

How to change the Keyboard layout on Solaris.

Hi,

I am running a machine with SunOS and NO graphical enviroment. Does someone know how to change the keyboard layout from the console??

Thanks

  • have a look at the loadkeys command

    pabloh84 : Thx, it accept the command but it doesn't actually do anything...
  • If it's running a recent Solaris 10 update release or patch set, or a release newer than Solaris 10 (such as Solaris Express or OpenSolaris), try kbd -s -- on older Solaris x86 releases, try kdmconfig.

    From alanc
  • Change the LAYOUT value in /etc/default/kbd (use "kbd -s" to get a list of allowed values). Then run "kbd -i" and finally reboot.

    From matli

.Net PostgreSQL Connection String

I am using PostgreSQL in a project I am working on, and one of the queries is timing out. I need to increase the timeout on the database connection, but since I am using my DAO through a complex wrapper to Active Record and NHibernate, I am not able to adjust the timeout of the command object - so I am hoping you can change the timeout through the connection string.

Any ideas?

  • Try this one:

    Provider=PostgreSQL OLE DB Provider;Data Source=myServerAddress;location=myDataBase;User ID=myUsername;password=myPassword;timeout=1000;

    Just replace the obvious parts (myUsername, myServerAddress, etc...) with your stuff.

    Also, for your reference, this site will give you connection string templates for pretty much any database on earth for pretty much any way you need to use it:

    http://www.connectionstrings.com

  • Have you tried to optimize the query? Optimizing is the best choice over increasing timeouts.

    Dana the Sane : I second this, explain analyse is your friend. Look for slow sequential scans and a poor join order, etc.
    From RedWolves
  • Npgsql-native connection string:

    Server=127.0.0.1;Port=5432;Userid=u;Password=p;Protocol=3;SSL=false;Pooling=false;MinPoolSize=1;MaxPoolSize=20;Timeout=15;SslMode=Disable;Database=test"
    
  • Found it: CommandTimeout=20;Timeout=15;

    From Ash