我正在尝试设置一个简单的hibernate应用程序,当我运行它时,我得到一个充满错误的堆栈跟踪。
我的pom.xml
文件中有以下maven依赖项:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.0.Final</version>
</dependency>
<!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.2</version>
</dependency>
我在本地运行的mysql版本是:
5.7.9, for osx10.11 (x86_64)
我正在运行,似乎是一种非常简单的方法,但仍然会出错:
// create session factory
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Model.class)
.buildSessionFactory();
// create session
Session session = factory.getCurrentSession();
Model newModel = new Model("....", "...", "...");
// start a transaction
session.beginTransaction();
// save the student object
System.out.println("Saving the model...");
session.save(newModel);
执行上面的操作后,我得到一个很长的堆栈跟踪... System.out.println也没有打印...所以连接似乎没有连接。
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Thu Jun 09 17:36:28 EST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:723)
at com.parkingapi.tests.Test.main(Test.java:17)
Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:105)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:123)
at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:41)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:106)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:40)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:19)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:138)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:110)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:74)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
... 14 more
Caused by: java.sql.SQLException: The server time zone value 'AEST' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:695)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:663)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:653)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:638)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:606)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:624)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:620)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:68)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1683)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:656)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:349)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)
at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
... 29 more
Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value 'AEST' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:73)
at com.mysql.cj.jdbc.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:118)
at com.mysql.cj.mysqla.MysqlaSession.configureTimezone(MysqlaSession.java:308)
at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:2474)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1817)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1673)
... 33 more
我在这里专注于这个错误:
服务器时区值'AEST'无法识别或代表更多 比一个时区。您必须配置服务器或JDBC 驱动程序(通过serverTimezone配置属性)使用更多 如果您想利用时区支持,请指定时区值。
我不确定我是如何解决它的?
打印出时区(System.out.println(TimeZone.getDefault());)打印以下详细信息:
sun.util.calendar.ZoneInfo [ID = “澳大利亚/墨尔本”,偏移= 36000000,dstSavings = 3600000,useDaylight =真,过渡= 142,lastRule = java.util.SimpleTimeZone中[ID =澳大利亚/墨尔本,偏移= 36000000,dstSavings = 3600000,useDaylight =真,startYear = 0,STARTMODE = 3,startMonth = 9,朝九特派= 1,startDayOfWeek = 1,开始时间= 7200000,startTimeMode = 1,endMode = 3,endMonth = 3,endday指定= 1 ,一个endDayOfWeek = 1,结束时间= 7200000,endTimeMode = 1]]
所以我查看了被标记的问题,并尝试更改我的连接字符串,但它仍然抛出相同的堆栈跟踪。下面是我的新连接网址。如您所见,我将serverTimezone更改为打印的Timezone.getDefault()
jdbc:mysql://localhost:3306/parking_hib?useLegacyDatetimeCode=false;serverTimezone=Australia/Melbourne;useSSL=false;
答案 0 :(得分:43)
尝试添加这些参数..
jdbc:mysql://localhost:3306/fussa?useLegacyDatetimeCode=false&serverTimezone=UTC
我正在使用 mysql-connector-java 6.0.4
答案 1 :(得分:24)
原来6.x
不兼容,转而5.1.39
修复它。
答案 2 :(得分:6)
正确的解决方案,即AEDT的恕我直言,如下更改URL
Cartfile
答案 3 :(得分:5)
jdbc:mysql://localhost:3306/fussa?useLegacyDatetimeCode=false&serverTimezone=UTC
使用&
代替&
。
您也可以尝试在mysql上更改时区:
SET GLOBAL time_zone = '00:00';
并查看当前设置:
SELECT @@global.time_zone, @@session.time_zone;
答案 4 :(得分:3)
抛出异常是因为MySql服务器时区(继承自系统时区)的格式与mysql连接器所期望的格式不同。您需要将MySql时区设置为“Australia / Sydney”。请查看详细的响应:Should MySQL have its timezone set to UTC?
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
SET GLOBAL time_zone = 'Australia/Sydney';
答案 5 :(得分:2)
build.gradle>
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.11'
application.properties>
spring.datasource.url=jdbc:mysql://localhost:3306/bookworm?useLegacyDatetimeCode=false&serverTimezone=UTC
-
mysql --version
mysql Ver 8.0.11 for osx10.13 on x86_64 (Homebrew)
这种组合对我有用。
答案 6 :(得分:2)
这是MySQLWorkbench版本的问题。升级到最新版本8.0,然后在jdbc URL中添加serverTimezone = UTC。
字符串url =“ jdbc:mysql:// localhost:3306 / your_db?useSSL = false&serverTimezone = UTC”;
答案 7 :(得分:1)
使用默认
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/web_student_tracker?user=root&password=&useLegacyDatetimeCode=false&serverTimezone=UTC");
答案 8 :(得分:1)
如果您使用Spring Boot,请尝试在localhost参数后添加一个问号,并添加以下内容:
?serverTimezone=UTC&useLegacyDatetimeCode=false
例如,我有一个名称为springboottutorial的mysql数据库。在此示例中,参数看起来像这样:
spring.datasource.url=jdbc:mysql://localhost:3306/springboottutorial?serverTimezone=UTC&useLegacyDatetimeCode=false
答案 9 :(得分:0)
嗨,您需要像下面这样在URL说明符中指定
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/jpahibernate?useLegacyDatetimeCode=false&serverTimezone=UTC");
dataSource.setUsername("root");
dataSource.setPassword("Password");
return dataSource;
}
在上面的代码段中,请查看setUrl
属性
答案 10 :(得分:0)
我有一个类似的问题,我使用的是mysql-connector-java 8.0.15(最新),但是对于某些原因,它仍然给我一个时区错误,我将其更改为mysql-connector-java 8.0.13,做到了。希望有用。
答案 11 :(得分:0)
在MySQL中:您可能会像下面这样使用以下用户:
第一个解决方案:将 serverTimezone = UTC 添加到连接网址
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC
第二个解决方案: n个MySQL my.ini 文件集默认时区如下所示的值
# Set default time zone
default-time-zone = '+08:00'
答案 12 :(得分:0)
我通过在数据库名称之后添加# SPICEY DICE Code snippet.
# - Create window
window2 = Tk()
window2.title("SPICEY DICE - Single Player Finish")
# - Label 1
Label(window2, text = "CONGRATULATIONS!").grid(row = 0, column = 0, sticky = W)
# - Label 2
Label(window2, text = "You have finished SPICEY DICE with a score of:").grid(row = 1, column = 0, sticky = W)
# - Text box 1
Output5 = Text(window2, width = 5, height = 1, wrap = WORD, background = "yellow")
Output5.grid(row = 2, column = 0, sticky = W)
# - Button 1
Button(window2, text = "LEADERBOARD", width = 15, command = SPLeaderboard).grid(row = 3, column = 0, sticky = W)
# - Label 3
Label(window2, text = "Close all of the windows to finish.").grid(row = 4, column = 0, sticky = W)
# - Run mainloop
window2.mainloop()
Output5.delete(0.0, END)
Output5.insert(END, str(TotalScore))
来解决此问题。我来自西海岸,区域名称为"?serverTimezone=America/Los_Angeles"
。如果不确定时区名称,请在RHEL / UBUNTU / CENTOS上的以下命令中运行
"America/Los_Angeles"
解决方案:
11:42 AM ✔ user1 sfo-server1 Δ [~] Ω timedatectl | grep "Time zone"
Time zone: America/Los_Angeles (PDT, -0700)
答案 13 :(得分:0)
加载时区数据后,
SET GLOBAL time_zone = 'Australia/Sydney';
为我工作。
就我而言,我希望设置是持久的,但这些都不适合我来保持时区,最后我使用 option in mysql doc 设置启动参数,如下所示,
--default-time-zone=Australia/Sydney
最常见的选项是在 my.cnf 中设置它,但这对我也不起作用。
注意:我的是 CentOS 8 和 MySql 8