【问题标题】:could not synchronize database state with session error while saving in groovy在 groovy 中保存时无法将数据库状态与会话错误同步
【发布时间】:2012-02-20 14:50:05
【问题描述】:

我在 groovy 控制器的保存方法中尝试保存/创建包时收到以下错误和堆栈跟踪。

Error 500: Executing action [save] of controller [se.accumulate.wizard.SubmissionController] caused exception:
  could not deserialize; nested exception is
    org.hibernate.type.SerializationException: could not deserialize java.io.StreamCorruptedException
      at java.util.Hashtable.reconstitutionPut(Hashtable.java:889)
      at java.util.Hashtable.readObject(Hashtable.java:861)
      at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
      at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1846)
      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
      at se.accumulate.wizard.PackagingService$$EnhancerByCGLIB$$2b5048f.createPackage(<generated>)

有关解决此问题的任何想法/提示。我已经检查了域与数据库等是否存在任何不一致。对我来说一切都很好。

示例代码:

def createPackage(Submission submission){
        logger.info("Creating submission package");
        def xml = createZip(submission);
    }

def createZip(Submission submission){
        def sw = new StringWriter()
        def xml = new groovy.xml.MarkupBuilder(sw);
        Customer customer = Customer.get(submission.customerId);
        String custDir = customer.ftpCustomerTempDirectory
        logger.info("Zip file: " + submission.fileName);

        final int BUFFER = 2048;
        try {
            String tmpFileName = custDir+"/" + java.util.UUID.randomUUID().toString().replaceAll("-", "") + ".zip";

            FileOutputStream fos = new FileOutputStream(tmpFileName);
            BufferedOutputStream dest;
            ZipOutputStream zos = new ZipOutputStream(fos);
            FileInputStream fis = new FileInputStream(custDir+"/" + submission.fileName);
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
            ZipEntry entry;
            while((entry = zis.getNextEntry()) != null) {
                //System.out.println("Extracting: " +entry);

                if (!entry.isDirectory()) {
                    if((entry.getName().toLowerCase().endsWith(".jar") || entry.getName().toLowerCase().endsWith(".jad") || entry.getName().toLowerCase().endsWith(".apk"))){
                        int count;
                        byte[] data = new byte[BUFFER];

                        String name = entry.getName();
                        if (name.indexOf('/') >= 0) {
                            String[] parts = name.split('/');
                            name = parts[parts.length - 1];
                        }
                        zos.putNextEntry(new ZipEntry("Applications/" + name));
                        while ((count = zis.read(data, 0, BUFFER))!= -1) {
                            zos.write(data, 0, count);
                        }
                        zos.closeEntry();
                    }
                }
            }
            zis.close();

            if (submission.applicationImage != null) {
                zos.putNextEntry(new ZipEntry("thumbnail_60x60.png"));
                zos.write(submission.applicationImage);
                zos.closeEntry();
            }

            zos.putNextEntry(new ZipEntry("Submission.xml"));
            zos.write(createXml(submission).toString().getBytes("UTF-8"));
            zos.close();


            FtpClientService fcs = new FtpClientService();
            fcs.putFile(customer.ftpUser, customer.ftpPassword, customer.ftpHost, customer.ftpPackagedDirectory, tmpFileName, submission.outputFileName);
            logger.info("check 1")
            if (!new File(tmpFileName).delete()) {
                logger.info("Could not delete tmp file " + tmpFileName);
            }
            logger.info("check 2")
            if (!new File(custDir+"/" + submission.fileName).delete()) {
                logger.info("Could not delete submission file " + submission.fileName);
            }
            logger.info("check 3")
        } catch(Exception e) {
            e.printStackTrace();
        }
        logger.info("check 4")
        return "sw";
    }

已显示所有日志检查 1 到 4,并且已创建预期的输出/文件,但已引发此异常...

我的域类:(Submission.groovy 扩展向导)

package se.accumulate.wizard

class Submission extends Wizard {
    long operatingSystemId
    long deploymentId
    int newOrExisting

    String applicationName
    String applicationShortName
    String reportingName
    byte[] applicationImage
    long contentProviderId

    String fileName
    String outputFileName

    Properties deviceMapping

    long applicationId

    int overwriteExistingApplicationDetails

    static constraints = {
        operatingSystemId (nullable:true)
        deploymentId (nullable:true)
        newOrExisting (nullable:true)
        applicationName (nullable:true)
        applicationShortName (nullable:true)
        reportingName (nullable:true)
        applicationImage (nullable:true, maxSize: 1048576)
        contentProviderId (nullable:true)
        fileName (nullable:true)
        deviceMapping (nullable:true, maxSize: 5242880)
        outputFileName (nullable:true)
    }
}

package se.accumulate.wizard

abstract class Wizard {
    Date dateCreated
    Date lastUpdated
    long createdByUserId
    long customerId
    Boolean isConfirmed = false
    int pageTracker
}

我的数据库表提交说明如下:(格式不正确,请见谅)

字段类型空键默认

'application_id', 'bigint(20)', 'NO', '', NULL, '' 'application_image'、'blob'、'YES'、''、NULL、'' 'application_name'、'varchar(255)'、'YES'、''、NULL、'' 'cms_id'、'varchar(255)'、'YES'、''、NULL、'' 'content_provider_id'、'bigint(20)'、'NO'、''、NULL、'' 'created_by_user_id'、'bigint(20) 无符号'、'NO'、''、NULL、'' 'date_created'、'datetime'、'NO'、''、NULL、'' 'deployment_id'、'bigint(20)'、'NO'、''、NULL、'' 'device_mapping', 'blob', 'YES', '', NULL, '' 'file_name'、'varchar(255)'、'YES'、''、NULL、'' 'is_confirmed', 'tinyint(1)', 'NO', '', NULL, '' 'last_updated'、'datetime'、'NO'、''、NULL、'' 'new_or_existing'、'int(11)'、'NO'、''、NULL、'' 'operating_system_id'、'bigint(20)'、'NO'、''、NULL、'' 'overwrite_existing_application_details'、'int(11)'、'NO'、''、NULL、'' 'customer_id'、'bigint(20)'、'YES'、'MUL'、NULL、'' 'output_file_name'、'varchar(255)'、'YES'、''、NULL、'' 'reporting_name'、'varchar(255)'、'YES'、''、NULL、'' 'application_short_name'、'varchar(255)'、'YES'、''、NULL、'' 'page_tracker', 'int(11)', 'YES', '', NULL, ''

将从 createPackage 调用的另一个方法的一部分(更改此方法后,出现错误)- 我刚刚过滤了“?”来自文件名

def createXml(Submission submission){
    Application application = Application.get(submission.getApplicationId());
    ContentProvider contentProvider = ContentProvider.get(submission.getContentProviderId());

    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element rootElement = doc.createElement("game");
    rootElement.setAttribute("title", submission.applicationName);
    rootElement.setAttribute("short", submission.applicationShortName);
    rootElement.setAttribute("externalRef", application.cmsId);
    rootElement.setAttribute("contentPartner", contentProvider.name);

    if (submission.applicationImage != null) {
        Element image = doc.createElement("image");
        image.setAttribute("type", "thumbnail");
        image.setAttribute("mime-type", "image/png");
        image.setAttribute("width", "60");
        image.setAttribute("height", "60");
        image.setTextContent("thumbnail_60x60.png");
        rootElement.appendChild(image);
    }

    Properties ps = submission.deviceMapping;
    Properties dm = (Properties) ps;
    for (def device : dm) {
        device.key = device.key.split("\\?")[0];
        logger.debug("key=${device.key}, value=${device.value}")
        if (!(device.value == null || "null".equalsIgnoreCase(device.value))) {
            logger.info("Adding handset: " + device.value);
            Element handset = doc.createElement("handset");
            handset.setAttribute("name", device.value);

            String name = device.key;
            if (name.indexOf('/') >= 0) {
                String[] parts = name.split('/');
                name = parts[parts.length - 1];
            }

            if (name.toLowerCase().endsWith(".jad")) {
                // java and blackberry
                Element jadFile = doc.createElement("jadfile");
                jadFile.setTextContent("Applications/" + name);
                handset.appendChild(jadFile);

                /*
                name = getJarFileName();
                if (name.indexOf('/') >= 0) {
                    String[] parts = name.split('/');
                    name = parts[parts.length - 1];
                }*/

                Element jarFile = doc.createElement("jarfile");
                jarFile.setTextContent("Applications/" + name.replace(".jad", ".jar"));
                handset.appendChild(jarFile);
            }
            else {
                // android
                Element jarFile = doc.createElement("jarfile");
                jarFile.setTextContent("Applications/" + name);
                handset.appendChild(jarFile);
            }

            rootElement.appendChild(handset);
        }
    }
    doc.appendChild(rootElement);

    StringWriter xmlString = new StringWriter();
    try{
        Result result = new StreamResult(xmlString);
        Source source = new DOMSource(doc);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);

    } catch (Exception e){
        e.printStackTrace();
    }
    logger.info("XML: " + xmlString.toString());

    return  xmlString;
}

【问题讨论】:

  • 对这个问题有什么想法吗?蒂姆...你有没有机会查看我的代码,你有没有发现我遗漏了什么?
  • 我认为我的电脑表现得很奇怪,并从另一台电脑上尝试了相同的代码,猜猜它的工作原理......嗯,不确定是什么设置在我的电脑中引发了这个错误。当我得到一些解决方案时,我会保持发布,同时欢迎任何其他输入

标签: grails groovy


【解决方案1】:

将createXml方法中的属性改为String,错误已消除....

Properties ps = submission.deviceMapping;
    Properties dm = (Properties) ps;
    for (def device : dm) {
        device.key = device.key.split("\\?")[0];

替换为

java.util.Properties dm = submission.deviceMapping;
        for (String fileName : dm.keys()) {
            String device = dm.getProperty(fileName);
            fileName = fileName.split("\\?")[0];

【讨论】:

    猜你喜欢
    • 2013-09-04
    • 2013-07-01
    • 2012-08-04
    • 2017-06-18
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 2011-04-18
    相关资源
    最近更新 更多