Index: xwiki-platform-core/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateBaseStore.java
===================================================================
--- xwiki-platform-core/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateBaseStore.java	(revision 6801)
+++ xwiki-platform-core/xwiki-core/src/main/java/com/xpn/xwiki/store/XWikiHibernateBaseStore.java	(working copy)
@@ -255,6 +255,7 @@
      */
     public void updateSchema(XWikiContext context) throws HibernateException
     {
+        log.debug("updateSchema: context.database is:"+context.getDatabase());
         updateSchema(context, false);
     }
 
@@ -323,22 +324,44 @@
      * @return the database/schema name.
      * @since XWiki Core 1.1.2, XWiki Core 1.2M2
      */
-    protected String getSchemaFromWikiName(String wikiName, XWikiContext context)
+    protected String getSchemaFromWikiName(String wikiName, XWikiContext context) throws XWikiException
     {
         if (wikiName == null) {
             return null;
         }
 
+
         DatabaseProduct databaseProduct = getDatabaseProductName(context);
 
-        if (databaseProduct == DatabaseProduct.DERBY) {
-            return wikiName.equalsIgnoreCase(context.getMainXWiki()) ? "APP" : wikiName.replace(
-                '-', '_');
-        } else if (databaseProduct == DatabaseProduct.HSQLDB) {
-            return wikiName.equalsIgnoreCase(context.getMainXWiki()) ? "PUBLIC" : wikiName
-                .replace('-', '_');
-        } else
-            return wikiName.replace('-', '_');
+        String retval  = wikiName.replace('-','_');
+        boolean isMain = wikiName.equalsIgnoreCase(context.getMainXWiki());
+               
+        
+        if (isMain) {
+            String configuredMainDatabaseSchemaName = XWiki.staticGetMainDatabaseSchemaName();
+            if (configuredMainDatabaseSchemaName.length()!=0) {
+                retval=configuredMainDatabaseSchemaName;
+            }else{
+                if (databaseProduct == DatabaseProduct.DERBY) {
+                   retval="APP";    
+                }else if (databaseProduct == DatabaseProduct.HSQLDB) {
+                   retval = "PUBLIC";                   
+                }
+            }
+        }else{
+            // virtual
+            String configurableVirtualDabaseSchemaPrefix = XWiki.staticGetVirtualDatabaseSchemaPrefix();
+            if (configurableVirtualDabaseSchemaPrefix.length()!=0) {
+                retval = configurableVirtualDabaseSchemaPrefix + retval;
+            }
+        }
+
+        if (log.isDebugEnabled()) {
+            log.debug("schema for wiki " + context.getDatabase()+" is "+retval);
+        }
+
+        return retval;
+                
     }
 
     /**
@@ -348,7 +371,7 @@
      * @return the database/schema name.
      * @since XWiki Core 1.1.2, XWiki Core 1.2M2
      */
-    protected String getSchemaFromWikiName(XWikiContext context)
+    protected String getSchemaFromWikiName(XWikiContext context) throws XWikiException
     {
         return getSchemaFromWikiName(context.getDatabase(), context);
     }
Index: xwiki-platform-core/xwiki-core/src/main/java/com/xpn/xwiki/XWiki.java
===================================================================
--- xwiki-platform-core/xwiki-core/src/main/java/com/xpn/xwiki/XWiki.java	(revision 6801)
+++ xwiki-platform-core/xwiki-core/src/main/java/com/xpn/xwiki/XWiki.java	(working copy)
@@ -207,6 +207,16 @@
     private String database;
 
     private String fullNameSQL;
+    
+    /**
+     * schema name for main database (cached from configuration)
+     */
+    private static String mainDatabaseSchema = null;
+    
+    /**
+     * database prefix for virtual wiki-s (cached from configuration)
+     */    
+    private static String virtualDatabaseSchemaPrefix = null;
 
     private URLPatternMatcher urlPatternMatcher = new URLPatternMatcher();
 
@@ -249,7 +259,7 @@
      */
     private static File tempDir = null;
 
-    private static String getConfigPath() throws NamingException
+    private static String getConfigPath() 
     {
         if (configPath == null) {
             try {
@@ -958,6 +968,24 @@
         return getConfig().getProperty(key);
     }
 
+    private static String  staticParam(String key) throws XWikiException
+    {        
+     String cfgpath = getConfigPath();   
+     try {    
+       FileInputStream fis = new FileInputStream(cfgpath);   
+       XWikiConfig tmpConfig = new XWikiConfig(fis);
+       return tmpConfig.getProperty(key);
+     }catch(FileNotFoundException ex){
+            Object[] args = {cfgpath};
+            throw new XWikiException(XWikiException.MODULE_XWIKI_CONFIG,
+                XWikiException.ERROR_XWIKI_CONFIG_FILENOTFOUND,
+                "Configuration file {0} not found",
+                ex,
+                args);         
+     }
+    }
+    
+    
     public String ParamAsRealPath(String key)
     {
         String param = Param(key);
@@ -1054,13 +1082,12 @@
     public void saveDocument(XWikiDocument doc, String comment, boolean isMinorEdit,
         XWikiContext context) throws XWikiException
     {
-        String server = null, database = null;
+        String serverDatabase = null, database = null;
         try {
-            server = doc.getDatabase();
-
-            if (server != null) {
+            serverDatabase = doc.getDatabase();
+            if (serverDatabase != null) {
                 database = context.getDatabase();
-                context.setDatabase(server);
+                context.setDatabase(serverDatabase);
             }
 
             // Setting comment & minoredit before saving
@@ -1081,7 +1108,7 @@
             getNotificationManager().verify(doc, originalDocument,
                 XWikiDocChangeNotificationInterface.EVENT_CHANGE, context);
         } finally {
-            if ((server != null) && (database != null)) {
+            if ((serverDatabase != null) && (database != null)) {
                 context.setDatabase(database);
             }
         }
@@ -1090,18 +1117,18 @@
     private XWikiDocument getDocument(XWikiDocument doc, XWikiContext context)
         throws XWikiException
     {
-        String server = null, database = null;
+        String serverDatabase = null, database = null;
         try {
-            server = doc.getDatabase();
+            serverDatabase = doc.getDatabase();
 
-            if (server != null) {
+            if (serverDatabase != null) {
                 database = context.getDatabase();
-                context.setDatabase(server);
+                context.setDatabase(serverDatabase);
             }
 
             return getStore().loadXWikiDoc(doc, context);
         } finally {
-            if ((server != null) && (database != null)) {
+            if ((serverDatabase != null) && (database != null)) {
                 context.setDatabase(database);
             }
         }
@@ -2250,7 +2277,10 @@
 
     public void setConfig(XWikiConfig config)
     {
-        this.config = config;
+        this.config = config;        
+        //lazy initialize configurable static variables.
+        getMainDatabaseSchemaName();
+        getVirtualDatabaseSchemaPrefix();
     }
 
     public void setStore(XWikiStoreInterface store)
@@ -3362,7 +3392,48 @@
     {
         this.database = database;
     }
+    
+        
+    public String getMainDatabaseSchemaName() 
+    {
+        if (mainDatabaseSchema == null) {
+            mainDatabaseSchema = Param("xwiki.db","");
+        }
+        return mainDatabaseSchema;
+    }
 
+    public static String staticGetMainDatabaseSchemaName() throws XWikiException
+    {
+        if (mainDatabaseSchema == null) {
+            mainDatabaseSchema = XWiki.staticParam("xwiki.db");
+            if (mainDatabaseSchema == null) {
+                mainDatabaseSchema = "";
+            }
+        }
+        return mainDatabaseSchema;
+    }
+
+    
+    public String getVirtualDatabaseSchemaPrefix()
+    {
+        if (virtualDatabaseSchemaPrefix == null){
+            virtualDatabaseSchemaPrefix = Param("xwiki.virtual.db.prefix","");
+        }
+        return virtualDatabaseSchemaPrefix;            
+    }
+
+    public static String staticGetVirtualDatabaseSchemaPrefix() throws XWikiException
+    {
+        if (virtualDatabaseSchemaPrefix == null){
+            virtualDatabaseSchemaPrefix = XWiki.staticParam("xwiki.virtual.db.prefix");
+            if (virtualDatabaseSchemaPrefix==null) {
+                virtualDatabaseSchemaPrefix = "";
+            }
+        }
+        return virtualDatabaseSchemaPrefix;            
+    }
+    
+    
     public void gc()
     {
         System.gc();
@@ -3485,8 +3556,9 @@
             sourceWiki = db;
 
         try {
-            if (sourceWiki != null)
+            if (sourceWiki != null) {
                 context.setDatabase(sourceWiki);
+            }
             XWikiDocument sdoc = getDocument(docname, context);
             if (!sdoc.isNew()) {
                 if (LOG.isInfoEnabled())
@@ -3494,8 +3566,9 @@
                         + " on wiki " + targetWiki);
 
                 // Let's switch to the other database to verify if the document already exists
-                if (targetWiki != null)
+                if (targetWiki != null) {
                     context.setDatabase(targetWiki);
+                }
                 XWikiDocument tdoc = getDocument(targetdocname, context);
                 // There is already an existing document
                 if (!tdoc.isNew()) {
