play freamwork 框架中 调用线程保存数据

package com.wanhua.thread;

import models.Log;
import play.db.jpa.JPAPlugin;

/**
 * 保存用户操作日志的线程
 *
 * @author w_xfpenga 2014-12-18
 *
 */
public class LogManagerThread extends Thread {

    private String content;// 日志内容
    private int type;// 日志类型:0为后台用户操作日志,1为移动用户操作日志
    private String operator;// 日志创建者
    private String path;// 操作路径,如:/users/delete/1

    /**
     * 构造函数初始化LogmanagerThread对象
     *
     * @param content
     * @param type
     * @param operator
     * @param path
     */
    public LogManagerThread(String content, int type, String operator, String path) {
        this.content = content;
        this.type = type;
        this.operator = operator;
        this.path = path;
    }

    /**
     * 重写线程中的run方法 将用户操作日志的保存到数据库中
     */
    @Override
    public void run() {
        String content = this.content;// 日志内容
        int type = this.type;// 日志类型:0为后台用户操作日志,1为移动用户操作日志
        String operator = this.operator;// 日志创建者
        String path = this.path;// 操作路径,如:/users/delete/1

        // 添加JPA环境
        JPAPlugin.startTx(false);
        // 保存用户到数据库
        Log.addLog(content, type, operator, path);
        JPAPlugin.closeTx(false);
    }
}

注意加上JPA环境:JPAPlugin.startTx(false);

原文地址:https://www.cnblogs.com/xunfang123/p/4171293.html