0
I’m trying to persist an object and I’m getting this error. Does anyone know how to solve? if you need the classes, persistence.xml, I send.
I am not running "on server" only by main method.
I’m not receiving data from the server
code:
package br.com.casadocodigo.jsfjpa.persistence;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import br.com.casadocodigo.jsfjpa.entities.Automovel;
public class PersistidorDeAutomovel {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
EntityManager em = emf.createEntityManager();
Automovel auto = new Automovel();
auto.setAnoFabricacao(2014);
auto.setMarca("Ferrari");
auto.setModelo("La Ferrari");
auto.setObservacoes("Nunca foi batido");
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(auto);
tx.commit();
em.close();
emf.close();
}
}
ago 04, 2016 6:12:10 pm org.hibernate.Annotations.common.Version INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final} Aug 04, 2016 6:12:10 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.2.2.Final} Aug 04, 2016 6:12:10 PM org.hibernate.cfg.Environment INFO: HHH000206: Hibernate.properties not found Aug 04, 2016 6:12:10 pm org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode Provider name : javassist Aug 04, 2016 6:12:10 PM org.hibernate.service.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl configure INFO: HHH000402: Using Hibernate built-in Connection pool (not for Production use!) Aug 04, 2016 6:12:10 PM org.hibernate.service.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl configure INFO: HHH000115: Hibernate Connection pool size: 20 Aug 04, 2016 6:12:10 pm org.hibernate.service.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl configure INFO: HHH000006: Autocommit mode: true Aug 04, 2016 6:12:10 PM org.hibernate.service.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl configure INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/automoveis] aug 04, 2016 6:12:10 PM org.hibernate.service.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl configure INFO: HHH000046: Connection properties: {user=root, password=***, autocommit=true, release_mode=auto} Aug 04, 2016 6:12:12 PM org.hibernate.engine.jdbc.Internal.Jdbcservicesimpl configure WARN: HHH000342: Could not obtain Connection to query Metadata : Communications link Failure
The last Packet sent successfully to the server was 0 milliseconds ago. The driver has not Received any packets from the server. aug 04, 2016 6:12:12 pm org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.Mysqlinnodbdialect Aug 04, 2016 6:12:12 PM org.hibernate.engine.jdbc.Internal.Lobcreatorbuilder useContextualLobCreation INFO: HHH000422: LOB contextual disabling Creation as Connection was null Aug 04, 2016 6:12:12 PM org.hibernate.engine.transaction.Internal.Transactionfactoryinitiator initiateService INFO: HHH000268: Transaction Strategy: org.hibernate.engine.transaction.Internal.jdbc.Jdbctransactionfactory Aug 04, 2016 6:12:12 pm org.hibernate.hql.internal.Ast.Astquerytranslatorfactory INFO: HHH000397: Using Astquerytranslatorfactory Aug 04, 2016 6:12:13 PM org.hibernate.Validator.internal.util.Version INFO: HV000001: Hibernate Validator 4.3.1.Final Aug 04, 2016 6:12:15 pm org.hibernate.engine.jdbc.spi.Sqlexceptionhelper logExceptions WARN: SQL Error: 0, Sqlstate: 08S01 Aug 04, 2016 6:12:15 pm org.hibernate.engine.jdbc.spi.Sqlexceptionhelper logExceptions ERROR: Communications link Failure
The last Packet sent successfully to the server was 0 milliseconds ago. The driver has not Received any packets from the server. Exception in thread "main" javax.persistence.Persistenceexception: org.hibernate.Exception.Jdbcconnectionexception: Could not open Connection at org.hibernate.ejb.AbstractEntityManagerImpl.Convert(Abstractentitymanagerimpl.java:1387) at org.hibernate.ejb.AbstractEntityManagerImpl.Convert(Abstractentitymanagerimpl.java:1310) at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(Abstractentitymanagerimpl.java:1397) at org.hibernate.ejb.TransactionImpl.Begin(Transactionimpl.java:62) at br.com.casadocodigo.jsfjpa.persistence.PersistidorDeAutomovel.main(Persistidordeautomovel.java:26) Caused by: org.hibernate.Exception.Jdbcconnectionexception: Could not open Connection at org.hibernate.Exception.internal.Sqlstateconversiondelegate.Convert(Sqlstateconversiondelegate.java:132) at org.hibernate.Exception.internal.Standardsqlexceptionconverter.Convert(Standardsqlexceptionconverter.java:49) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.Convert(Sqlexceptionhelper.java:125) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.Convert(Sqlexceptionhelper.java:110) at org.hibernate.engine.jdbc.Internal.LogicalConnectionImpl.obtainConnection(Logicalconnectionimpl.java:221) at org.hibernate.engine.jdbc.Internal.LogicalConnectionImpl.getConnection(Logicalconnectionimpl.java:157) at org.hibernate.engine.transaction.Internal.jdbc.Jdbctransaction.doBegin(Jdbctransaction.java:67) at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.Begin(Abstracttransactionimpl.java:160) at org.hibernate.Internal.SessionImpl.beginTransaction(Sessionimpl.java:1426) at org.hibernate.ejb.TransactionImpl.Begin(Transactionimpl.java:59) ... 1 more Caused by: com.mysql.jdbc.exceptions.jdbc4.Communicationsexception: Communications link Failure
The last Packet sent successfully to the server was 0 milliseconds ago. The driver has not Received any packets from the server. at sun.reflect.Nativeconstructoraccessorimpl.newInstance0(Native Method) at sun.reflect.Nativeconstructoraccessorimpl.newInstance(Unknown Source) at sun.reflect.Delegatingconstructoraccessorimpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.SQLError.createCommunicationsException(Sqlerror.java:1116) at com.mysql.jdbc.MysqlIO.(Mysqlio.java:344) at com.mysql.jdbc.ConnectionImpl.coreConnect(Connectionimpl.java:2332) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(Connectionimpl.java:2369) at com.mysql.jdbc.ConnectionImpl.createNewIO(Connectionimpl.java:2153) at com.mysql.jdbc.ConnectionImpl.(Connectionimpl.java:792) at com.mysql.jdbc.Jdbc4connection.(Jdbc4connection.java:47) at sun.reflect.Nativeconstructoraccessorimpl.newInstance0(Native Method) at sun.reflect.Nativeconstructoraccessorimpl.newInstance(Unknown Source) at sun.reflect.Delegatingconstructoraccessorimpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.ConnectionImpl.getInstance(Connectionimpl.java:381) at com.mysql.jdbc.NonRegisteringDriver.connect(Nonregisteringdriver.java:305) at org.hibernate.service.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl.getConnection(Drivermanagerconnectionproviderimpl.java:204) at org.hibernate.Internal.Abstractsessionimpl$Noncontextualjdbcconnectionaccess.obtainConnection(Abstractsessionimpl.java:292) at org.hibernate.engine.jdbc.Internal.LogicalConnectionImpl.obtainConnection(Logicalconnectionimpl.java:214) ... 6 more Caused by: java.net.Connectexception: Connection refused: connect at java.net.Dualstackplainsocketimpl.connect0(Native Method) at java.net.Dualstackplainsocketimpl.socketConnect(Unknown Source) at java.net.Abstractplainsocketimpl.doConnect(Unknown Source) at java.net.Abstractplainsocketimpl.connectToAddress(Unknown Source) at java.net.Abstractplainsocketimpl.connect(Unknown Source) at java.net.Plainsocketimpl.connect(Unknown Source) at java.net.Sockssocketimpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.(Unknown Source) at java.net.Socket.(Unknown Source) at com.mysql.jdbc.StandardSocketFactory.connect(Standardsocketfactory.java:257) at com.mysql.jdbc.MysqlIO.(Mysqlio.java:294) ... 21 more
I think it would be interesting for you to enter your code in the question statement ...
– stringnome
send your persistence.xml, for better analysis
– Marcos Sousa
Beforehand, I see no need to close the "emf". emf.close();
– Marcos Sousa
I solved the problem, I did not have the Mysql package installed on the pc. Thanks for the help
– Gabriel Faria