Search in sources :

Example 76 with DbConnection

use of com.zimbra.cs.db.DbPool.DbConnection in project zm-mailbox by Zimbra.

the class MailboxManager method createMailboxInternal.

private synchronized Mailbox createMailboxInternal(OperationContext octxt, Account account, boolean isGalSyncAccount) throws ServiceException {
    CreateMailbox redoRecorder = new CreateMailbox(account.getId());
    Mailbox mbox = null;
    boolean success = false;
    DbConnection conn = DbPool.getConnection();
    try {
        CreateMailbox redoPlayer = (octxt == null ? null : (CreateMailbox) octxt.getPlayer());
        int id = (redoPlayer == null ? Mailbox.ID_AUTO_INCREMENT : redoPlayer.getMailboxId());
        // create the mailbox row and the mailbox database
        MailboxData data;
        boolean created = false;
        try {
            data = DbMailbox.createMailbox(conn, id, account.getId(), account.getName(), -1);
            ZimbraLog.mailbox.info("Creating mailbox with id %d and group id %d for %s.", data.id, data.schemaGroupId, account.getName());
            created = true;
        } catch (ServiceException se) {
            if (MailServiceException.ALREADY_EXISTS.equals(se.getCode())) {
                // mailbox for the account may be created by other server, re-fetch now.
                id = DbMailbox.getMailboxId(conn, account.getId());
                if (id > 0) {
                    data = DbMailbox.getMailboxStats(conn, id);
                } else {
                    throw ServiceException.FAILURE("could not create mailbox", se);
                }
            } else {
                throw se;
            }
        }
        mbox = account.isIsExternalVirtualAccount() ? instantiateExternalVirtualMailbox(data) : instantiateMailbox(data);
        mbox.setGalSyncMailbox(isGalSyncAccount);
        // the existing Connection is used for the rest of this transaction...
        mbox.beginTransaction("createMailbox", octxt, redoRecorder, conn);
        if (created) {
            // create the default folders
            mbox.initialize();
        }
        // cache the accountID-to-mailboxID and mailboxID-to-Mailbox relationships
        cacheAccount(data.accountId, data.id);
        cacheMailbox(mbox);
        redoRecorder.setMailboxId(mbox.getId());
        success = true;
    } catch (ServiceException e) {
        // Log exception here, just in case.  If badness happens during rollback
        // the original exception will be lost.
        ZimbraLog.mailbox.error("Error during mailbox creation", e);
        throw e;
    } catch (OutOfMemoryError e) {
        Zimbra.halt("out of memory", e);
    } catch (Throwable t) {
        ZimbraLog.mailbox.error("Error during mailbox creation", t);
        throw ServiceException.FAILURE("createMailbox", t);
    } finally {
        try {
            if (mbox != null) {
                mbox.endTransaction(success);
            } else {
                conn.rollback();
            }
        } finally {
            conn.closeQuietly();
        }
    }
    return mbox;
}
Also used : DbMailbox(com.zimbra.cs.db.DbMailbox) CreateMailbox(com.zimbra.cs.redolog.op.CreateMailbox) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) CreateMailbox(com.zimbra.cs.redolog.op.CreateMailbox) MailboxData(com.zimbra.cs.mailbox.Mailbox.MailboxData) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 77 with DbConnection

use of com.zimbra.cs.db.DbPool.DbConnection in project zm-mailbox by Zimbra.

the class MailboxManager method getMailboxSizes.

/** Returns the zimbra IDs and approximate sizes for all mailboxes on
     *  the system.  Note that mailboxes are created lazily, so there may be
     *  accounts homed on this system for whom there is is not yet a mailbox
     *  and hence are not included in the returned <code>Map</code>.
     *
     * @throws ServiceException  The following error codes are possible:<ul>
     *    <li><code>service.FAILURE</code> - an error occurred while accessing
     *        the database; a SQLException is encapsulated</ul> */
public Map<String, Long> getMailboxSizes(List<NamedEntry> accounts) throws ServiceException {
    List<Integer> requested;
    synchronized (this) {
        if (accounts == null) {
            requested = new ArrayList<Integer>(mailboxIds.values());
        } else {
            requested = new ArrayList<Integer>(accounts.size());
            for (NamedEntry account : accounts) {
                Integer mailboxId = mailboxIds.get(account.getId());
                if (mailboxId != null)
                    requested.add(mailboxId);
            }
        }
    }
    DbConnection conn = null;
    try {
        conn = DbPool.getConnection();
        return DbMailbox.getMailboxSizes(conn, requested);
    } finally {
        if (conn != null)
            DbPool.quietClose(conn);
    }
}
Also used : NamedEntry(com.zimbra.cs.account.NamedEntry) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 78 with DbConnection

use of com.zimbra.cs.db.DbPool.DbConnection in project zm-mailbox by Zimbra.

the class MailboxUpgrade method upgradeTo2_7.

/** Create MUTED flag in TAG table. */
public static void upgradeTo2_7(Mailbox mbox) throws ServiceException {
    DbConnection conn = DbPool.getConnection(mbox);
    try {
        if (flagExists(conn, mbox, Flag.FlagInfo.MUTED))
            return;
        DbTag.createTag(conn, mbox, Flag.FlagInfo.MUTED.toFlag(mbox).mData, null, false);
        conn.commit();
    } catch (ServiceException e) {
        conn.rollback();
        throw e;
    } finally {
        conn.closeQuietly();
    }
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 79 with DbConnection

use of com.zimbra.cs.db.DbPool.DbConnection in project zm-mailbox by Zimbra.

the class MailboxUpgrade method upgradeTo2_1.

public static void upgradeTo2_1(Mailbox mbox) throws ServiceException {
    DbConnection conn = DbPool.getConnection(mbox);
    try {
        if (alreadyUpgradedTo2_1(conn, mbox)) {
            ZimbraLog.mailbox.warn("detected already-migrated mailbox %d during migration to version 2.1; skipping.", mbox.getId());
        } else {
            // for flags that we want to be searchable, put an entry in the TAG table
            for (int tagId : Mailbox.REIFIED_FLAGS) {
                DbTag.createTag(conn, mbox, Flag.of(mbox, tagId).mData, null, false);
            }
            migrateFlagColumn(conn, mbox, false);
            migrateTagColumn(conn, mbox, false);
            // the tag load when the Mailbox object was constructed returned no tags
            //   because we hadn't migrated the tags yet, so force a reload
            mbox.purge(MailItem.Type.TAG);
            // any items already in the item cache don't have their tags set because
            //   we hadn't migrated the tags when they were loaded, so purge them
            mbox.purge(MailItem.Type.CONTACT);
        }
        conn.commit();
    } catch (ServiceException e) {
        conn.rollback();
        throw e;
    } finally {
        conn.closeQuietly();
    }
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 80 with DbConnection

use of com.zimbra.cs.db.DbPool.DbConnection in project zm-mailbox by Zimbra.

the class MailboxUpgrade method upgradeTo2_4.

/** Create POST flag in TAG table. */
public static void upgradeTo2_4(Mailbox mbox) throws ServiceException {
    DbConnection conn = DbPool.getConnection(mbox);
    try {
        if (flagExists(conn, mbox, Flag.FlagInfo.POST))
            return;
        DbTag.createTag(conn, mbox, Flag.FlagInfo.POST.toFlag(mbox).mData, null, false);
        conn.commit();
    } catch (ServiceException e) {
        conn.rollback();
        throw e;
    } finally {
        conn.closeQuietly();
    }
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Aggregations

DbConnection (com.zimbra.cs.db.DbPool.DbConnection)218 PreparedStatement (java.sql.PreparedStatement)165 SQLException (java.sql.SQLException)162 ResultSet (java.sql.ResultSet)85 Mailbox (com.zimbra.cs.mailbox.Mailbox)71 ArrayList (java.util.ArrayList)36 ServiceException (com.zimbra.common.service.ServiceException)23 UnderlyingData (com.zimbra.cs.mailbox.MailItem.UnderlyingData)18 HashMap (java.util.HashMap)10 Metadata (com.zimbra.cs.mailbox.Metadata)9 IOException (java.io.IOException)9 MailItem (com.zimbra.cs.mailbox.MailItem)8 TypedIdList (com.zimbra.cs.mailbox.util.TypedIdList)8 AccountServiceException (com.zimbra.cs.account.AccountServiceException)7 Folder (com.zimbra.cs.mailbox.Folder)7 List (java.util.List)6 DbVolume (com.zimbra.cs.db.DbVolume)5 PendingDelete (com.zimbra.cs.mailbox.MailItem.PendingDelete)5 VirtualConversation (com.zimbra.cs.mailbox.VirtualConversation)5 CreateVolume (com.zimbra.cs.redolog.op.CreateVolume)5