Tomcat loadbalancing序列化异常PersistenceExceptionTranslationInterceptor:延迟初始化异常

时间:2011-11-10 11:03:01

标签: java tomcat serialization load-balancing session-replication

我必须在负载均衡架构中放置两个tomcat服务器(tcat01和tcat02)。

我正在使用tomcat 6.x,我在tomcat tcat01上编辑了这样的conf / server.xml:

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tcat01">
     <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6"/>
      ....

在tcat02上,conf / server.xml是这样的:

 <Engine name="Catalina" defaultHost="localhost" jvmRoute="tcat02">
     <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6"/>
      ....

我启动了tcat01,然后是tcat02,il catalina.out似乎tcat01与tcat02通信良好。

然后我用网络导航器连接到webapp,然后每次我在webapp中做一些事情(我的意思是当我导航时)都有这个例外:

  Nov 24, 2011 12:00:13 AM org.apache.catalina.core.ContainerBase backgroundProcess
WARNING: Exception processing manager org.apache.catalina.ha.session.DeltaManager@278c4835 background process
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.mycompany.myproject.model.authentification.Authority.groups, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
    at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:332)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)

以下是无法进行sezialized的类的代码(即堆栈跟踪中提到的java bean):

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

@Entity
@Table(name = "authority", uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class Authority implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = -7436593455923433675L;

//SYSTEM ROLE CONSTANT
public static final String MANAGER_SYSTEM_ROLE = "ROLE_MANAGER";
public static final String CLIENT_SYSTEM_ROLE = "ROLE_CLIENT";
public static final String PEDAGO_ADMIN_SYSTEM_ROLE = "ROLE_PEDAGO_ADMIN";

private Integer id;
@Size(min=1)
@Pattern(regexp="^ROLE[_a-zA-Z]+$", message="{authority.name.pattern.error}")
private String name;
@Size(max=65535)
private String description;


private Boolean isSystem;

private Set<Group> groups = new HashSet<Group>(0);

public Authority() {
    this.isSystem = false;
}

public Authority(String name) {
    this.name = name;
    this.isSystem = false;
}

public Authority(String name, String description, Set<Group> groups) {
    this.name = name;
    this.description = description;
    this.groups = groups;
    this.isSystem = false;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}

@Column(name = "name", unique = true, nullable = false, length = 45)
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

@Column(name = "description")
public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public void setIsSystem(Boolean isSystem) {
    this.isSystem = isSystem;
}

@Column(name = "system")
public Boolean getIsSystem() {
    return isSystem;
}

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "group_authorities", joinColumns = { @JoinColumn(name = "authority_id", nullable = false, updatable = true) }, inverseJoinColumns = { @JoinColumn(name = "group_id", nullable = false, updatable = true) })
public Set<Group> getGroups() {
    return this.groups;
}

public void setGroups(Set<Group> groups) {
    this.groups = groups;
}

@PrePersist
protected void updateSystemField() {
    if(isSystem == null)
        isSystem = false;
}
}

这是java bean组的代码(因为我们在一组Groups上有一个懒惰的初始化异常):

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;


@Entity
@Table(name = "groups", uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class Group implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = 7380068327050752558L;

private Integer id;
@NotNull
@Size(min=1)
private String name;
private String description;

private Boolean isSystem;

private Set<Authority> authorities = new HashSet<Authority>(0);
private Set<User> users = new HashSet<User>(0);

public Group() {
    this.isSystem = false;
}

public Group(String name) {
    this.name = name;
    this.isSystem = false;
}

public Group(String name, String description, Set<Authority> authorities, Set<User> users) {
    this.name = name;
    this.description = description;
    this.isSystem = false;
    this.authorities = authorities;
    this.users = users;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}

@Column(name = "name", unique = true, nullable = false, length = 45)
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

@Column(name = "description")
public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public void setIsSystem(Boolean isSystem) {
    this.isSystem = isSystem;
}

@Column(name = "system")
public Boolean getIsSystem() {
    return isSystem;
}

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "group_authorities", joinColumns = { @JoinColumn(name = "group_id", nullable = false, updatable = true) }, inverseJoinColumns = { @JoinColumn(name = "authority_id", nullable = false, updatable = true) })
public Set<Authority> getAuthorities() {
    return this.authorities;
}

public void setAuthorities(Set<Authority> authorities) {
    this.authorities = authorities;
}

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "group_users", joinColumns = { @JoinColumn(name = "group_id", nullable = false, updatable = true) }, inverseJoinColumns = { @JoinColumn(name = "user_id", nullable = false, updatable = true) })
public Set<User> getUsers() {
    return this.users;
}

public void setUsers(Set<User> users) {
    this.users = users;
}

public void addAuthority(Authority authority) {
    if(authorities == null)
        authorities = new HashSet<Authority>(0);
    authorities.add(authority);
}

public boolean removeAuthority(Authority authority) {
    return authorities == null || authorities.remove(authority);
}

public void addUser(User user) {
    if(users == null)
        users = new HashSet<User>(0);
    users.add(user);
}

public boolean removeUser(User user) {
    return users == null || users.remove(user);
}

@PrePersist
protected void updateSystemField() {
    if(isSystem == null)
        isSystem = false;
}
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您正在获取延迟实例化异常,因为您将权限组映射为lazy。在复制会话时,您处于活动会话之外,因此无法保持水分。要解决此问题,您可以选择以下几种方法:

  1. 更改映射,以便急切地获取关联
  2. 将DAO更改为步入该集合(或使用hibernate.initialize)以在会话关闭之前强制执行延迟加载
  3. 从会话中分离对象,然后在将对象放入会话之前将对象显式设置为null,以替换那里的hibernate代理。
  4. 对于这个特例,选项1或2可能是最干净的。