Olá a todos,
Tenho uma aplicação que se conecta no postgresql, estou usando o hibernate para a persitencia dos dados.
Tenho uma classe HibernateCore onde recupero uma sessão ativa e também tenho os meus daos. Na minha aplicação tenho uma classe que extende uma Thread entao dentro dela processo algumas informações. Coloco minha thread em um pool onde o tamanho é de 40 execuções. Andei observando o profile da aplicação via Netbeans e classe Object cresce muito bem como a classe FlushEntityEvent, não se está certo a maneira que estou gerenciando as sessões do Hibernate, alguem se habilita a dar uma olhada nas minhas classes?
HibernateCore
public class HibernateCore {
private static SessionFactory sessionFactory;
private static final ThreadLocal threadSession = new ThreadLocal();
private static final ThreadLocal threadTransaction = new ThreadLocal();
static {
try {
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
public static Session getSession() {
Session s = (Session) threadSession.get();
try {
if (s == null) {
s = sessionFactory.openSession();
threadSession.set(s);
}
} catch (HibernateException ex) {
ex.printStackTrace();
}
return s;
}
public static void closeSession() {
try {
Session s = (Session) threadSession.get();
threadSession.set(null);
if (s != null && s.isOpen()) {
s.close();
}
} catch (HibernateException ex) {
ex.printStackTrace();
}
}
public static void beginTransaction() {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx == null) {
tx = getSession().beginTransaction();
threadTransaction.set(tx);
}
} catch (HibernateException ex) {
ex.printStackTrace();
}
}
public static void commitTransaction() {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.commit();
}
threadTransaction.set(null);
} catch (HibernateException ex) {
ex.printStackTrace();
}
}
public static void rollbackTransaction() {
Transaction tx = (Transaction) threadTransaction.get();
try {
threadTransaction.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.rollback();
}
} catch (HibernateException ex) {
ex.printStackTrace();
} finally {
closeSession();
}
}
public AnnotationConfiguration getAnnotationConfiguration() {
return new AnnotationConfiguration().configure();
}
Classe Main que instancia a as minhas Threads
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException {
int maxThreads = 0;
Properties properties = new Properties();
try {
properties.load(new FileInputStream("zhone.properties"));
maxThreads = Integer.parseInt(properties.getProperty("max_threads"));
} catch (FileNotFoundException ex) {
Log.logger.severe("Erro ao carregar o arquivo de configurações do programa\n" + ex.getMessage() + "\n");
System.exit(1);
} catch (IOException ex) {
Log.logger.severe("Erro ao carregar o arquivo de configurações do programa\n" + ex.getMessage() + "\n");
System.exit(1);
}
DaoShelf daoShelf = new DaoShelf(HibernateCore.getSession());
List<Shelf> shelves = daoShelf.listByModel("ZhoneMalc");
shelves.addAll(daoShelf.listByModel("ZhoneRaptor"));
TransportMapping transportMapping = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transportMapping);
List<ThreadZhoneInventory> threads = new ArrayList<ThreadZhoneInventory>();
for(Shelf s : shelves){
threads.add(new ThreadZhoneInventory(s, snmp));
}
ExecutorService threadPool = Executors.newFixedThreadPool(maxThreads);
for(ThreadZhoneInventory t: threads){
threadPool.execute(t);
}
threadPool.shutdown();
}
}
classe thread
public class ThreadZhoneInventory extends Thread {
private Shelf shelf;
private ZhoneSnmp zhoneSnmp;
private ZhoneSystem zhoneSystem;
private ZhoneCard zhoneCard;
private ZhoneAdslPort zhoneAdslPort;
public ThreadZhoneInventory(Shelf shelf, Snmp snmp) {
this.shelf = shelf;
this.zhoneSnmp = new ZhoneSnmp(shelf.getIp(), "161", "ZhonePrivate", "2c", snmp);
this.zhoneSystem = new ZhoneSystem(zhoneSnmp.getCore());
this.zhoneCard = new ZhoneCard(zhoneSnmp.getCore());
this.zhoneAdslPort = new ZhoneAdslPort(zhoneSnmp.getCore());
}
public void persistenceCards() {
DaoCard dao = new DaoCard(HibernateCore.getSession());
DaoCardTypeZhone daoTypeZhone = new DaoCardTypeZhone(HibernateCore.getSession());
try {
List<Card> cards = this.zhoneCard.getCards();
if (cards.size() != 0) {
List<Card> oldCards = dao.getCardsByIp(shelf.getIp());
for (Card c : oldCards) {
HibernateCore.beginTransaction();
dao.getDaoGeneric().delete(c);
HibernateCore.getSession().flush();
HibernateCore.commitTransaction();
}
for (Card c : cards) {
c.setShelf(shelf);
c.setLastUpdate(Calendar.getInstance());
HibernateCore.beginTransaction();
try {
if (daoTypeZhone.getCardTypeZhone(c.getCardTypeZhone().getId().getTypeZhone(), c.getCardTypeZhone().getId().getLineType()) == null) {
daoTypeZhone.getDaoGeneric().saveOrUpdate(c.getCardTypeZhone());
}
dao.getDaoGeneric().save(c);
HibernateCore.getSession().flush();
HibernateCore.commitTransaction();
} catch (DaoException ex) {
Log.logger.severe("[HIBERNATE] - erro ao gravar os dados do elemento: " + shelf.getIp() + "\n" + ex.getMessage() + "\n");
}
}
}
} catch (DaoException ex) {
Logger.getLogger(ThreadZhoneInventory.class.getName()).log(Level.SEVERE, null, ex);
} catch (ZhoneException ex) {
Log.logger.severe("[ZHONE] - erro ao recuperar os dados do elemento: " + shelf.getIp() + "\n" + ex.getMessage() + "\n");
}
}
public void persistencePorts() {
Dao<Port> dao = new Dao<Port>(HibernateCore.getSession(), Card.class);
try {
List<Port> ports = zhoneAdslPort.getPorts();
for (Port p : ports) {
p.getId().setShelf(shelf);
HibernateCore.beginTransaction();
try {
dao.saveOrUpdate(p);
HibernateCore.getSession().flush();
} catch (DaoException ex) {
Log.logger.severe("[HIBERNATE] - erro ao gravar os dados do elemento: " + shelf.getIp() + "\n" + ex.getMessage() + "\n");
}
HibernateCore.commitTransaction();
}
} catch (ZhoneException ex) {
Log.logger.severe("[ZHONE] - erro ao recuperar os dados do elemento: " + shelf.getIp() + "\n" + ex.getMessage() + "\n");
}
}
public void run() {
try {
this.persistenceCards();
this.persistencePorts();
this.finalize();
} catch (Throwable ex) {
Logger.getLogger(ThreadZhoneInventory.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Att,
Paulo