Monday, April 25, 2011

Using Volume Shadow Copy Service (VSS) in Delphi

Does anyone have sample code to copy open (in-use and locked by another program) files using Volume Shadow Copy Service (VSS) API?

There was an article about this in The Delphi Magazine (September 2005). But that code is no longer available and even if it was, it's not current.

I am looking for sample code that works with current version of Volume Shadow Copy service libraries.

From stackoverflow

How to create an XmlMappingSource during runtime?

(Follow-Up-Question to How to change LINQ O/R-M table name/source during runtime?)

I need to change the table source of a LINQ 2 SQL O/R-Mapper table during runtime. To achieve this, I need to create an XmlMappingSource. On command line, I could use SqlMetal to create this mapping file, but I would like to create the mapping file during runtime in memory. The XmlMappingSource is a simple xml file, looking something like this:

<?xml version="1.0" encoding="utf-8"?>
<Database Name="MyDatabase" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007">
  <Table Name="dbo.MyFirstTable" Member="MyFirstTable">
    <Type Name="MyFirstTable">
      <Column Name="ID" Member="ID" Storage="_ID" DbType="UniqueIdentifier NOT NULL" IsPrimaryKey="true" IsDbGenerated="true" AutoSync="OnInsert" />
      <Association Name="WaStaArtArtikel_WaVerPreisanfragen" Member="WaStaArtArtikel" Storage="_WaStaArtArtikel" ThisKey="ArtikelID" OtherKey="ID" IsForeignKey="true" />
    </Type>
  </Table>
  <Table Name="dbo.MySecondTable" Member="MySecondTable">
    <Type Name="MySecondTable">
      <Column Name="ID" Member="ID" Storage="_ID" DbType="UniqueIdentifier NOT NULL" IsPrimaryKey="true" IsDbGenerated="true" AutoSync="OnInsert" />
      <Column Name="FirstTableID" Member="FirstTableID" Storage="_FirstTableID" DbType="UniqueIdentifier NOT NULL" />
      <Association Name="MySecondTable_MyFirstTable" Member="MyFirstTable" Storage="_MyFirstTable" ThisKey="FirstTableID" OtherKey="ID" IsForeignKey="true" />
    </Type>
  </Table>
</Database>

This should be possible to create using reflection, for example I can get the database name from a data context like this:

using System.Data.Linq.Mapping;
using System.Xml.Linq;

XDocument mapWriter = new XDocument();
DatabaseAttribute[] catx = (DatabaseAttribute[])typeof(WcfInterface.WaDataClassesDataContext).GetCustomAttributes(typeof(DatabaseAttribute), false);
XElement xDatabase = new XElement("Database");
xDatabase.Add(new XAttribute("Name", catx[0].Name));
mapWriter.Add(xDatabase);

My problem: I can't find good documentation of the mapping, so extracting the necessary information is quite error-prone - maybe someone can point me to good docs of the mapping, or, even better, to a code example how to create the mapping file?

From stackoverflow
  • Have you considered using LINQ to Entities, the mapping formats for LINQ to Entities are documented.

    Sam : Actually, I was not really aware of L2E as a replacement for L2S. So, in L2E it is possible to just change the mapping during runtime?
    Richard : I would not consider L2E as a /replacement/, but as the "big brother". L2S is simple, but limited; L2E is much more functional but harder to use.
    Sam : So, how would I change the table name in L2E than?
    Richard : I know you /can/ swap mappings, just not /how/. Well until I have sometime available to really dig into L2E.
  • Use Damien Guard's Open Source T4 templates. They do everything SQLMetal can do and more, and you'll have the full T4 engine behind you.

    Sam : Uhm, maybe you can explain how this would help me for my problem?

JSP vs Velocity what is better?

What is better between JSP and velocity in - Performance - Ease of use - Ease of creating reusable components - Availability of open source 3rd parties - IDE support

From stackoverflow
  • Velocity or even better FreeMarker. In JSP you cannot have runtime dispatch for pojo hierarchies and everything is statically typed which is a pain. Moreover if you create many JSP2.0 custom tags (say more than 100-150) then your development-deployment cycle will slow down heavily due to inefficiencies of Jasper to resolve dependencies efficiently.

    On the other hand JSP has great tool support.

    slow JSP compilation references:

    http://www.mailinglistarchive.com/users@tomcat.apache.org/msg10786.html

    http://marc.info/?l=tomcat-dev&m=119377083422720&w=2

    Chii : i've always wondered why freemarker is considered better than velocity - to me they seem to do exactly the same thing! I havent used velocity before, but I have seen it in bits. I've used freemarker, and i have to say its pretty easy to use. But i expected velocity to be similar, thus my wondering.
    cherouvim : FreeMarker is more advanced and it can go to more low level than Velocity, and with great power comes great responsibility ;) http://freemarker.org/fmVsVel.html
  • I'll focus on using a template engine, because that is what I have most experience with.

    It depends on what you really want to do. Servlets in combination with Velocity (or FreeMarker for that matter) offer a very good seperation of logic and presentation. Templates are harder to test, because you would need to evaluate the template to be able to judge wheter the HTML (or whatever else the output format is) is correct. For JSP this can be done in your IDE of choice.

    The big advantage of templates is that you can store these completely outside of your application and even update them while your application is running. This is something that is a little harder to do with JSP, although hot deployment comes pretty close.

    Reusable components can be created by using the include functionality of the template engine.

  • Advantages of Velocity:

    • strict separation of view from business logic
    • simple syntax that can be understood by graphic designers
  • @Vartec: I don't think that the "strict separation of view from business logic" is a velocity feature that is not present in jsp. You can do business logic in jsp (more or less) but it's not recommended at all. But I agree in your point regarding the syntax.

    Performance

    JSP is compiled to Java, so I don't think that velocity is faster. (have not done benchmarks myself)

    Ease of use

    For designers: velocity For programmers: (IMHO) jsp, because it's closer to code

    Ease of creating reusable components

    JSP has lots of components Velocity has no components itself (not component oriented)

    Availability of open source 3rd parties

    I have seen far more projects using JSP or JSP related technologies than velocity. Maybe because velocity is really low level... :-)

    IDE support

    There are plenty of tools for jsp. Especially the eclipse jboss plugin/tool suite has a good jsp editor.

    Plugins for Velocity are mostly not functional or pretty basic (you get lucky if you have syntax highlighting)

  • I don't know whether Velocity can be able to compete with JSP in all aspects but Velocity is faster and nuch easier.Efficiency of Velocity is 35 to 45% more if it is complicated webpages it may be reduced but still it is 5% more than JSP.

    Dan Howard : If you mean Velocity is faster to work with then OK but Velocity is NOT faster than JSP. Velocity merges the html string with the data while JSP sends html directly down the writer. If you add logging times around velocity's mergerTemplate call you'll see what I mean.

How to replace a plus character using Java's String.replaceAll method

What's the correct regex for a plus character (+) as the first argument (i.e. the string to replace) to Java's replaceAll method in the String class? I can't get the syntax right.

From stackoverflow
  • You'll need to escape the + with a \ and because \ is itself a special character in Java strings you'll need to escape it with another \.

    So your regex string will be defined as "\\+" in Java code.

    I.e. this example:

    String test = "ABCD+EFGH";
    test = test.replaceAll("\\+", "-");
    System.out.println(test);
    
  • You need to escape the + for the regular expression, using \.

    However, Java uses a String parameter to construct regular expressions, which uses \ for its own escape sequences. So you have to escape the \ itself:

    "\\+"
    
    Aaron Maenpaa : I have the bad habit of using '/'s when building the regex and then running .replace('/', '\\'), so that I don't have to type "\\\\" to match a literal backslash.
    gustafc : If you want to replace a fixed string, Pattern.quote(String) is a very nice friend.
  • when in doubt, let java do the work for you:

    myStr.replaceAll(Pattern.quote("+"), replaceStr);
    
    John Topley : That's a nice technique - thanks.
  • If you want a simple string find-and-replace (i.e. you don't need regex), it may be simpler to use the StringUtils from Apache Commons, which would allow you to write:

    mystr = StringUtils.replace(mystr, "+", "plus");
    
    Gerhard Dinhof : thx for pointing to this. helped me remembering using this non-regex solution in simple cases.
    Vinze : isn't that equivalent to using mystr.replace("+", "plus") ? replace does not use regex (while replaceAll does).

GDI performance on Windows mobile or CE device

I have a Windows CE application that uses a lot of vector graphics and in places is quite slow. I'm currently using GDI for rendering via a bitmap for flicker free refreshes. Typically, I'm windowing in on part of a large 3d map. On some devices (e.g. 166mhz SH4), this gets slow with 3-5 second refresh times for big datasets. My question is this;

  • Has anyone done any comparisons on the relative speed of graphic operations on Windows mobile versus Win32. Put another way, are profiling results from a Win32 version of the software applicable to a WinCE version, assuming we are only looking a GDI calls.

  • Has anyone tried profiling onboard on a WinCE platform (C++ app), if yes, using what tools.

  • Is anyone aware of any methods to improve drawing speed on Windows CE. I'm currently looking at FastGraph following feedback from a previous question, but this is a slightly longer term solution. Bad and all as it is, I'm looking for something faster to implement for an upcoming release.

From stackoverflow
  • I've done a lot of this kind of benchmarking, and GDI operations are slower on WinCE than regular Win32, but only slower in proportion to the slower processors on WinCE devices. In other words, there doesn't seem to be any additional performance hit from using GDI in WinCE.

    Sorry, I don't have answers to your last two questions.

    Shane MacLaughlin : Thanks and very useful. Once I know that from a proportional standpoint Win32 and WinCE are at least similar, I can profile effectively from the desktop. My concern was the graphics cards on the PC might skew the results to the extent I would optimize incorrectly.
  • I don't have a lot of knowledge of the graphics side of things but from experience, if you want to be fast at some specific hardware related things, the closer to "metal" the faster you can get (and the harder it gets!). So you could look into using Direct Draw or Direct 3D (altho I think they are dropping D3D and going over to OpenGL ES for WM7). You may like to look into way Game Developers use.

    On the question of Profiles, I haven't found any but I do build my own.

    OregonGhost : On modern devices, you can use OpenVG instead of OpenGL, which supplies hardware-accelerated vector-graphics and a relatively fast software fallback, though I think the latter is not available by default (i.e. in worst case, you have to buy one).

Why does my .NET application crash when run from a network drive?

My .NET application fails when run from a network drive even when the very same executable runs perfectly fine from a local hard drive?

I tried checking for "Full trust" like so:

try
{
    // Demand full trust permissions
    PermissionSet fullTrust = new PermissionSet( PermissionState.Unrestricted );
    fullTrust.Demand();

    // Perform normal application logic

}
catch( SecurityException )
{
    // Report that permissions were not full trust
    MessageBox.Show( "This application requires full-trust security permissions to execute." );
}

However, this isn't helping, by which I mean the application starts up and the catch block is never entered. However, a debug build shows that the exception thrown is a SecurityException caused by an InheritanceDemand. Any ideas?

From stackoverflow
  • Did you try Using CasPol to Fully Trust a Share?

    Joel Coehoorn : Almost upvote-worthy, but it provides the solution without explaining the problem.
  • If this is .NET 2.0 or greater, ClickOnce was created to really help with this deployment stuff. I only deploy to network shares using that.

  • This is security built in by microsoft into the .net framework. It's a way of stopping malware to be run locally with full priviliges, so you cannot change this programmatically in the code.

    What you need to do is increase the trust of specific assemblies. You do this in the .NET Framework Configuration (Control Panel->Administrative Tools), and has to be done on each computer.

    As with any security measures, it's a pain-in-the-ass, but will help the world to be less infected etc...

    Epaga : but why does his message box not show up?
    Davy Landman : ...stopping malware... How many malware have you found written in .NET? Any non .NET executable will be able to run from the network using full privileges (by default). The only difference is that .NET did not allow it by default while windows does.
    : well, blocking managed code while still allowing Win32 binaries to be executed is not a security measure...
  • You may have already done this, but you can use CasPol.exe to enable FullTrust for a specified network share.

    For example

    cd c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
    CasPol.exe -m -ag 1.2 -url file:///N:/your/network/path/* FullTrust
    

    More info here.

  • It indeed has to do with the fact the apps on a network location are less trusted then on your local hdd (due to the default policy of the .NET framework).

    If I'm not mistaken Microsoft finally corrected this annoyance in .NET 3.5 SP1 (after a lot of developers complaining).

    I google'd it: .NET Framework 3.5 SP1 Allows managed code to be launched from a network share!

    Paul Smith : Verified this by having the affected users download the Service Pack, and all is good. Thanks!
    Jason Down : Excellent! I've had to use CasPol before with a utility we created for some of our customers. It's a pain having to create a script and have it run before your utility is called, just because it's run from a network location.

SQL Server backup/restore v.s. detach/attach

Hello everyone,

I have one database which contains the most recent data, and I want to replicate the database content into some other servers. Dues to non-technical reasons, I can not directly use replicate function or sync function to sync to other SQL Server instances.

Now, I have two solutions, and I want to learn the pros and cons for each solution. Thanks!

Solution 1: detach the source databse which contains the most recent data, then copy to the destination servers which need the most recent data, and attach database at the destination servers;

Solution 2: make a full backup of source server for the whole database, then copy data to destination servers and take a full recovery at the destination server side.

thanks in advance, George

From stackoverflow
  • The Detach / Attach is often quicker than performing a backup as it doesn't have to create a new file. Therefore therefore the time from server a to server b is almost purely the file copy time.

    The Backup / Restore option allows you to perform a full backup, restore than, then perform a differential backup which means your down time can be reduced between the two.

    If its data replication you're after, does that mean you want the database functional in both locations? In that case, you probably want the backup / restore option as that will leave the current database fully functional.

    EDIT: Just to clarify a few points. By downtime, I mean that if you're migrating a database from one serer to another. You generally will be stopping people using it whilst it's in transit. Therefore, from the "stop" point on Server A up to the "start" point on Server B this could be considered downtime. Otherwise, any actions performed on the database on server A during transit will not be replicated onto server B.

    In regard to the "create a new file". If you detach a database you can copy the MDF file immediately. It's already there ready to be copied. However, if you perform a backup, you have to wait for the .BAK file to be created and then move it to it's new location for a restore. Again this all comes down to is this a snapshot copy or a migration.

    George2 : Two confusions: 1. "it doesn't have to create a new file" -- new file you mean? 2. "down time can be reduced between the two" -- why there is downtime? I think for SQL Server duing full backup and full recovery model, there is no downtime for both source/destination server?
    George2 : "If its data replication you're after, does that mean you want the database functional in both locations?" -- both source and destination server could endure downtime, but I want to keep downtime of destination server as short as possible. Any new advice about the best solution?
    George2 : Thanks Robin, read your edited comments. So the new file you mean .bak file? Another question, when using attach/detach, will there be any transaction logs at both source database server or destination database server?
    Robin Day : On pressing Detach... The database is taken offline. It will no longer be part of Server A. It will only become available on Server B once you Attach it. I think what you're describing is replicating a snapshot of your db on a second server. Therefore, backup / restore is the way to go.
    George2 : Thanks Robin, I think for attach/detach operation themselves, will there be any transaction log generated? How about backup/recovery, any additional transaction log generated?
    Robin Day : There will be no additional transaction log, just the mdf and ldf files. The database is taken offline when detaching so no more data is processed
  • Solution 2 would be my choice... Primarily becuase it won't create any downtime on the source database. The only disadvatage i can see is that depending on the database recovery model, the transaction log will be truncated meaning if you wanted to restore any data from the transaction log you'd be stuffed, you'd have to use your backup file.

    EDIT: Found a nice link; http://sql-server-performance.com/Community/forums/p/5838/35573.aspx

    George2 : I can ensure during backup of source database, there is no insert/delete/update operations, and on destination database, it is readonly all the time (all modifications are on source database). So, in my case no transaction log for both full backup on source server
    George2 : and full recovery on destination server?
    George2 : when using attach/detach, will there be any transaction logs at both source database server or destination database server?